모델

2014-03-25 2 views
0

내가 내 범위를 오염하고 생각하지만 난 알아낼 수 없습니다 수정 angular.js 지침에 범위를 오염 방지하는 방법 방법 또는 그것을 방지하는 방법 :모델

를 내가 두 번 호출되는 지침을 따라서 다른 파라미터 :

<div profile-summary attributes="user.attributes" sentiment="positive" limit="3" threshold="20"> </div> 
<div profile-summary attributes="user.attributes" sentiment="negative" limit="3" threshold="-20"> </div> 

그것은 scope.attributes 오름차순 정렬의 상부 3리스트를 출력한다 (만약 감정 = "양성") (음의 경우) 또는 내림차순.

app.directive('profileSummary', function() { 
    var features; 
    return { 
     restrict: "A", 
     scope: { 
      attributes: '=', 
      sentiment: '@', 
      threshold: '=', 
      limit: '='}, 
     template: '<h5>{{title}}</h5><ol><li ng-repeat="attr in attributes">{{attr.categorical_value}}</li></ol>', 
     link: function (scope, element, attrs) { 
      //do stuff to 

      if (scope.sentiment == 'positive') { 
       scope.title = 'Loves'; 
       features = _.sortBy(scope.attributes, function (f) { 
        return (100000 - f.reactivity) 
       }); 
       features = _.filter(features, function (f) { 
        return f.reactivity > scope.threshold 
       }); 
      } else { 
       scope.title = 'Hates'; 
       features = _.sortBy(scope.attributes, function (f) { 
        return (f.reactivity) 
       }); 
       features = _.filter(features, function (f) { 
        return f.reactivity < scope.threshold 
       }); 
      } 
      features.length = scope.limit; 


      scope.attributes = features; //am I polluting global scope? how to avoid? 
     } 
    } 

문제는 제 지시자가 제 출력 중복이다 :

이에 의해 지향성 부른다. 첫 번째 지시문을 제거하면 올바른 (다른) 출력을 얻게됩니다.

답변

1

한 가지 방법은 속성 배열 대신 지시문 템플릿에 features 배열을 사용하는 것으로 수정하는 방법을 생각할 수 있습니다. 이 방법은 속성 배열 지금

template: '<h5>{{title}}</h5><ol><li ng-repeat="attr in features">{{attr.categorical_value}}</li></ol>', 

감동 그래서이

scope.attributes = features; //delete

+0

퍼펙 필요하지 않습니다이

scope.features = _.sortBy(scope.attributes, function (f) {

같은 지침의 범위에 정의되어야한다 제공되지 않을 것이다! 고마워요. (btw,'scope'에 $) – metalaureate