search

Home  >  Q&A  >  body text

angular.js - angular 的指令参数如何理解link

function link(scope, iElement, iAttrs, controller) { ... }

谁能够举个例子来说说明一些,angular的指令中attr具体是指哪里吗?

下面的attrs.data这中的data如何获取,或者设置在哪里?在<demo></demo>如何写

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

return{
        
       template: '<p></p>',
        link : function(scope, element, attrs){            
            if($.trim(attrs.data).length>0){
            
            
            }
        },
       }

}
)



html:

<demo></demo>
给我你的怀抱给我你的怀抱2744 days ago494

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 17:06:05

    The execution time of the link function is after angular compiles this template. 4 parameters:

    1. scope The scope of the current directive, whether it is independently determined by the scope parameter

    2. element The dom element of the current directive is wrapped with angular.element(element) to form a jqlite/jquery object

    3. Attributes corresponding to the attrs directive. For example

    <demo data='some data'></demo>

    The attrs.data is 'some data' which is hard-coded. If you want to bind it, it must be scoped independently.

    1. controller is the method provided by the required directive. If multiple are required, controller will be an array.

    reply
    0
  • Cancelreply