如何在指令中使用绑定?

时间:2013-09-10 10:46:27

标签: angularjs angularjs-directive angularjs-ng-repeat

在我的ng-repeat中,我有一个名为update的指令,它接受当前重复索引。如何将索引值传递给我的指令?

这就是我的标记的样子:

<ul>
    <li ng-repeat="article in articles">
         <span class="btn" update="{{ $index }}">update</span>
         <span class="btn" ng-attr-update="{{ $index }}">update</span>
    </li>
</ul>

指令:

angular.module('magazine')
.directive('update', function() {
    return {
        restrict: 'A', // restricting it only to attributes
        link: function(scope, element, attrs) {
            console.log(attrs.update);
        }
    }
})

当我运行上面的代码时,它会记录undefined。指令(http://docs.angularjs.org/guide/directive)上的Angular docs表示使用ng-attr作为属性的前缀可以完成工作。但它没有发生。我检查了我的DOM,当我使用ng-attr-<directive>="{{ $index }}"作为属性的前缀时,它不会将其编译为仅<directive>="value"

我有什么遗漏或做错了吗?

0 个答案:

没有答案