Angularjs: how to pass scope variables to a directive?

前端 未结 3 501
误落风尘
误落风尘 2021-02-01 17:19

I am trying to use directive to create and append several tags to a

as shown below:

module.directive(\'createControl\', function(){
  re         


        
3条回答
  •  滥情空心
    2021-02-01 17:40

    Read Attributes/observing interpolated attributes section of the directive docs. During the link phase the attributes haven't been set.

    There are several ways including using attrs.$observe or $timeout.

    app.directive('createControl', function($timeout){
     return function(scope, element, attrs){
          attrs.$observe('createControl',function(){
            console.log(' type:',attrs.createControl);
             element.text('Directive text, type is: '+attrs.createControl);
          });
      }
    }) ;
    

    DEMO

提交回复
热议问题