AngularJS : Pass $scope variable as directive attribute

后端 未结 4 1846
失恋的感觉
失恋的感觉 2021-02-13 14:27

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:

4条回答
  •  天涯浪人
    2021-02-13 15:22

    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

提交回复
热议问题