Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  127] [ 7]  / answers: 1 / hits: 48127  / 10 Years ago, tue, february 10, 2015, 12:00:00

I'm trying to pass the $scope variable values to a custom directive as attribute, but it's not working.



Here is the HTML code:



<ul ng-repeat=q in questions>
<li>
{{q.question}}
<check-list name={{q.id}}></check-list>
</li>
</ul>


The directive is <check-list name={{q.id}}></check-list>, and here is the directive code :



    app.directive('checkList',function(){
return {
restrict:'E',
template: function(elem,attrs){
console.log(attrs.name);
return '</br> <input type=radio /> Yes </br> <input type=radio /> No'
},
link:function(scope,elem,attrs){

}
};
})


I'm logging the attribute attrs.name but the value I'm getting is {{q.id}} instead of the actual value of q.id


More From » angularjs

 Answers
36

I suppose what you want to do is injecting scope object from controller to your directive. So you can define your directive as



app.directive('checkList',function(){
return {
restrict:'E',
scope: {
name: =
}
template: '{{name}}</br> <input type=radio /> Yes </br> <input type=radio /> No',
link:function(scope,elem,attrs){

}
};
}


And in your view, you can reference your directive as



<check-list name=q.id></check-list>

[#67890] Friday, February 6, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reina

Total Points: 241
Total Questions: 82
Total Answers: 94

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
reina questions
;