【问题标题】:Filtering angular repeats过滤角度重复
【发布时间】:2013-04-03 06:09:34
【问题描述】:

尝试为 Angular.js 创建过滤器

Controller.js 示例 sn-p:

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;
};

【问题讨论】:

    标签: angularjs angularjs-ng-repeat


    【解决方案1】:

    我用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
       }
    ];}
    

    【讨论】:

      【解决方案2】:

      请看plunkerhttp://plnkr.co/edit/SkXnZhwic8KHWvj0JyFI?p=preview

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        • 2015-08-24
        • 1970-01-01
        • 2017-06-20
        相关资源
        最近更新 更多