【问题标题】:angularjs ng-repeat filter based on array length基于数组长度的angularjs ng-repeat过滤器
【发布时间】:2015-04-08 12:43:28
【问题描述】:

我想知道,如何将过滤器放入一个只会带回具有颜色的项目的 ng-repeat?我希望在网格上方有一个名为“显示带有颜色的”的复选框,它会在选中时根据颜色数组的计数过滤列表,并在未选中时显示全部。

{
   "_id": "54d13c3f3c25d5d8123a1d62",
   "name": "Barry",
   "colours": ["239, 101, 128"]
},
{
   "_id": "54d13sfg5d5d8hgf6gg",
   "name": "John",
   "colours": []
},
{
   "_id": "34d13sfg5d5d4tt6g",
   "name": "Andrew",
   "colours": []
},
{
   "_id": "44d165d5d4t77t6g",
   "name": "Gary",
   "colours": ["25, 234, 22", "5, 100, 255"]
},

【问题讨论】:

    标签: angularjs filter


    【解决方案1】:

    我认为您可以使用控制器功能来测试颜色的存在 所以在haml/coffeescript中

    %tr{"ng-repeat2 => "item in list | filter: hasColour(item)"}
    

    hasColour 是一个控制器函数

     $scope.hasColour=  (item) ->
      item.colours.length > 0
    

    恐怕未经测试

    【讨论】:

    • 模板语法为filter: hasColour
    【解决方案2】:

    以下内容会给每个人都没有颜色:

    <div ng-repeat="item in items | filter: { colours: '!' }">
    

    再次否定它,你会让每个人都有颜色:

    <div ng-repeat="item in items | filter: { colours: '!!' }">
    

    演示: http://plnkr.co/edit/oIl3ohe0TLMWcQlTPuY5?p=preview

    【讨论】:

    • 太完美了,谢谢。我不知道'!和'!!'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 2015-12-20
    • 2018-01-28
    • 2014-02-10
    相关资源
    最近更新 更多