$index的ngRepeat里面angularjs指令
我试图找到最好的方法来获得一个ngRepeat使用自定义指令的索引位置。我想解决的问题是,对于ngRepeat的每次迭代,我想知道我在迭代中的位置,所以我可以创建一个基于索引位置的自定义模板的选项。
除非有更好的方法这样做,我试图做这个功能基于这个文档从指令:
http://docs.angularjs.org/guide/directive 我很难理解这句话告诉我什么。所以,我试图做的是这… 首先,ngRepeat指令与我的指令一起使用在“testTemplate”中: <ul> <li my-attr="test()" my-directive ng-repeat="value in values" class="span2"> </li> </ul> 接下来,我的指令定义: angular.module('myModule',[]) .directive('myDirective',function() { return { replace : true,transclude : true,scope : { test: '&myAttr' },templateUrl: 'partials/myTemplate.html',link : function(scope,element,attrs) { alert(scope.count); } } }); 现在终于,我试图定义的控制器内的“测试”函数为引用指令的父模板。 function TestTemplateCtrl($scope,testTemplate) { $scope.test = function() { alert('in test'); $scope.count = "1"; } } 所以第一个问题是我的“测试”函数在父中没有被执行。也许我不明白如何应该调用父控制器中的测试函数。现在我实际上还没有增加,但是是上面的正确的方式,我会去增加我的计数,当我到达那一点,如果我可以得到测试功能开火?
而不是scope。$ parent。$ index你可以考虑传递$ index作为指令属性:
<li my-directive index="{{$index}}" ng-repeat="value in values"> 指示: myApp.directive('myDirective',function() { return { replace: true,// transclude: true,scope: { index: '@' },template: '<div>testing {{index}}</div>',link: function(scope,attrs) { // ... } } }); Fiddle。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |