2013-07-14 18 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

我不認爲你可以使用納克級表達式過濾功能,但你可以簡單地定義控制器內的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 –