2013-07-14 3 views
1

$ location.search() 매개 변수에 내 결과 집합을 필터링하려면 어떻게합니까?

<div ng-repeat="tag in tags"> 
    <div ng-class="{active:tagged(tag.id)}" ng-click="filter(tag.id)"> 
    {{album.title}} 
    </div> 
</div> 

태그가 지정된 결과가 true이면 클래스 active이 추가됩니다.

filter('tagged', function() { 
    return function (id) { 
    //Assume "tags" is also the name of the parameter. 
    //"tags" is being set by $location.search({tags:['123','456','789']}); 
    return ($location.search().tags.indexOf(id) != -1)?true:false; 
    }; 
}); 

ng-class 내부의 규칙은 잘못하지만 지식 격차를 입증하는 예로서 사용하고 있습니다.

답변

2

난 당신이 NG-클래스 표현에서 필터 기능을 사용할 수 있다고 생각하지 않습니다,하지만 당신은 단순히 컨트롤러 내부에 tagged 함수를 정의 할 수 있습니다 이것에 대한

app.controller('AppController', 
    [ 
     '$scope', 
     '$location', 
     function($scope, $location) { 
     $location.search('tags', [1, 3]); 

     $scope.tags = [ 
      {id: 1, title: "Can't Hold Us"}, 
      {id: 2, title: "Just Give Me A Reason"}, 
      {id: 3, title: "Mirrors"}, 
      {id: 4, title: "Get Lucky"}, 
     ]; 

     $scope.tagged = function(id){ 
      return $location.search().tags.indexOf(id) !== -1; 
     }; 

     } 
    ] 
); 
<body ng-controller="AppController"> 

    <div ng-repeat="tag in tags"> 
    <div ng-class="{active:tagged(tag.id)}"> 
     {{tag.title}} 
    </div> 
    </div> 

</body> 

PLUNKER

+0

+1 . 실제 테스트는 URL 매개 변수의 상태가 변경되면 bool이 클래스를 적절히 업데이트 할 것인지를 제어합니다. 대답은 '예'입니다. http://plnkr.co/edit/s4RNrl?p=preview –