2013-02-08 4 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

내가 거기에 추가했습니다. 미안 해요, 대답하는 경향이 많습니다. –

+1

@drew는 컨트롤러 범위에'mowers' 속성이 있다는 것을 나타 냈습니다. 그래서 링크 함수가 새로운 자식 범위 (부모 범위에서 숨김/그림자)에 다른 함수를 만드는 이유가 확실하지 않습니다. 상위 범위의 속성에 액세스하기 만하면됩니다. –

+0

무슨 뜻인지 알 겠어. 좋은 캐치. 내 마음 속에서 그는 '범위 : {}'를하고있었습니다. –