Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  123] [ 1]  / answers: 1 / hits: 51151  / 11 Years ago, sat, december 28, 2013, 12:00:00

I am trying to pass a $scope's variable to a directive, but its not working. I am catching the variable in the template function:



app.directive('customdir', function () {

return {
restrict: 'E',

template: function(element, attrs) {
console.log(attrs.filterby);
switch (attrs.filterby) {
case 'World':
return '<input type=checkbox>';
}
return '<input type=text />';
}
};
});


What I need is the value of variable filterby not the variable name itself.



Plunkr Demo


More From » angularjs

 Answers
23

Or like this



app.directive('customdir', function ($compile) {
var getTemplate = function(filter) {
switch (filter) {
case 'World': return '<input type=checkbox ng-model=filterby>';
default: return '<input type=text ng-model=filterby />';
}
}

return {
restrict: 'E',
scope: {
filterby: =
},
link: function(scope, element, attrs) {
var el = $compile(getTemplate(scope.filterby))(scope);
element.replaceWith(el);
}
};
});


http://plnkr.co/edit/yPopi0mYdViElCKrQAq9?p=preview


[#73517] Thursday, December 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
angelr

Total Points: 399
Total Questions: 96
Total Answers: 101

Location: Finland
Member since Sun, May 21, 2023
1 Year ago
angelr questions
Tue, Sep 14, 21, 00:00, 3 Years ago
Mon, Dec 30, 19, 00:00, 5 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;