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>

このクラスactiveは、タグ付けの結果が true の場合に追加されます。

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は間違っていますが、知識のギャップを示す例として使用しています。

4

1 に答える 1

2

ng-class 式でフィルター関数を使用できるとは思いませんが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>

プランカー

于 2013-07-14T10:31:59.933 に答える