2013-02-08 144 views
1

我試圖在指令中使用ngRepeat - 但我真的不知道該怎麼做。我關門了嗎?Angular JS - 在模板中ngRepeat

割草機是我的控制器中使用此指令的數組。

.directive('slider', function() { 
return function() { 
    template: '<slider><film><frame ng-repeat="mower in mowers">{{mower.series}}</frame></film></slider>'; 
    replace: true; 
     scope: true; 
    link: function postLink(scope, iElement, iAttrs) { 
      // jquery to animate 
     } 
    } 
}); 

回答

1

是的,你很接近,但語法是關閉的。你需要在你的示波器上指定mowers

.directive('slider', function() { 
return { 
    restrict: 'E', //to an element. 
    template: '<film><frame ng-repeat="mower in mowers">{{mower.series}}</frame></film>', 
    replace: true, 
    scope: true, // creates a new child scope that prototypically inherits 
    link: function postLink(scope, iElement, iAttrs) { 
      scope.mowers = [ 
       { series: 1 }, 
       { series: 2 }, 
       { series: 3 } 
      ]; 
     } 
    } 
}); 

此外,您自己不需要參考<slider>

上面的代碼還假定您已經爲<film><frame>發出了指令。

最後,如果你想割草機陣列從外部範圍傳遞,你會改變你的範圍設置爲這樣:

scope: { 
    mowers: '=' 
} 

然後你可以設置它像這樣:

<slider mowers="myMowers"></slider> 

其中myMowers是您的控制器或父指令的作用域上的變量。

我希望有幫助。

+0

我在那裏補充說。對不起,我傾向於回答,然後修改很多。 – 2013-02-08 18:22:38

+1

@drew表示控制器作用域具有'mowers'屬性,所以我不確定爲什麼鏈接函數在新的子作用域上創建另一個(它將隱藏/隱藏父作用域上的作用域),而不是隻需訪問父範圍的屬性即可。 – 2013-02-09 01:06:59

+0

我明白你的意思了。接得好。在我看來,他在做一個'範圍:{}'。 – 2013-02-09 06:21:13

相關問題