过滤角重复

时间:2013-04-02 21:02:40

标签: angularjs angularjs-ng-repeat

尝试为Angular.js

创建过滤器

Controller.js示例代码段:

function TitleListCtrl($scope) {
  $scope.titles = [
    { "name": "Foobar",
      "editions": 
        [{"print": true, 
          "ebook": false,
          "audio": false }]
    },
   { "name": "FooBarBar",
      "editions": 
        [{"print": false, 
          "ebook": false,
          "audio": false }]
   }
];}

Angular html:

<ul ng-controller="TitleListCtrl">
        <li class="TitleList" ng-repeat="title in titles 
                                         | filter:isUpcoming 
                                         | orderBy:'date'">{{title.name}}</li>
</ul>

现在,我正在尝试仅返回那些没有活动版本的标题(版本数组的成员都没有设置为true)。发现很难在网上找到这样的例子......

$scope.isUpcoming = function(title) {
return title.editions[0] & title.editions[1] & title.editions[2]===false;
};

2 个答案:

答案 0 :(得分:0)

我使用data-ng-show代替上述方法:

<ul ng-controller="TitleListCtrl">
<li class="TitleList" data-ng-show="!(title.upcoming)" 
                                    ng-repeat="title in titles 
                                    | filter:query 
                                    | orderBy:'date'">{{title.name}}</li>
</ul>

并为需要的标题添加了一对新"upcoming": true,

function TitleListCtrl($scope) {
  $scope.titles = [
    { "name": "Foobar",
  "editions": 
    [{"print": true, 
      "ebook": false,
      "audio": false }]
    },
   { "name": "FooBarBar",
  "editions": 
    [{"print": false, 
      "ebook": false,
      "audio": false }],
  "upcoming": true
   }
];}

答案 1 :(得分:0)

请查看plunker http://plnkr.co/edit/SkXnZhwic8KHWvj0JyFI?p=preview

它实现了一个自定义过滤器,可以将长列表过滤到即将到来的列表,然后呈现即将到来的列表