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:
Or pass the entire scope to your directive:
app.directive('checkList',function(){
return {
restrict:'E',
scope: true, //scope
template: function(elem,attrs){
console.log(attrs.name);
return ' Yes No'
},
link:function(scope,elem,attrs){
var question = scope.q; //get your question here
}
};
})
I recommend you pass only reference type as argument to your directive. Do not pass primitive types (q.id
may be an integer). Pass question
instead. It's all about how angularjs utilizes prototypical inheritance.
Scope
is a complex topic in angularjs. See this: https://github.com/angular/angular.js/wiki/Understanding-Scopes