2013-05-08 96 views
37

如果我有一个指令myDir,我把它叫做内ng-repeat像这样传递变量角度指令

<my-dir myindex="{{$index}}"></my-dir> 

如何访问myindex?当我在postLink函数中使用attrs.myindex时,我得到实际字符串{{$index}}。当我检查html时,它实际上是myindex="2"

回答

61

尝试

<my-dir myindex="$index"></my-dir> 

然后

app.directive('myDir', function() { 
    return { 
    restrict: 'E', 
    scope: { 
     myindex: '=' 
    }, 
    template:'<div>{{myindex}}</div>', 
    link: function(scope, element, attrs){ 
     console.log('test', scope.myindex) 
    } 
    }; 
}) 

演示:Plunker