【问题标题】:How to filter ng-repeat by a nested object's property如何通过嵌套对象的属性过滤 ng-repeat
【发布时间】:2014-11-20 22:39:53
【问题描述】:

我有以下数组:

  var boofers = [
    {
        "id" : "kurfki8z6786",
        "query" : {"name": "foo", "status": "open"},
        "uid" : "1416422209395",
    },
    {
        "id" : "kurfki8z6874",
        "query" : {"name": "puff", "status": "closed"},
        "uid" : "1416422209387",
    },
    {
        "id" : "kurfki8z6123",
        "query" : {"name": "joe", "status": "closed"},
        "uid" : "1416422209301",
    }
  ]

我需要打印boofersstatusstatusboofer 对象为closed,所以我正在尝试以下但没有得到结果,因为没有打印任何内容{{boofer.uid}}:

<div ng-repeat="boofer in boofers | filter: {query.status: 'closed'}">{{boofer.uid}}</div>

谁能告诉我我在这里犯的错误以及如何解决它?

【问题讨论】:

    标签: angularjs filter ng-repeat


    【解决方案1】:

    从 JSON 数组中的每个 id 中删除 )

    boofers = [
        {
            "id" : "kurfki8z6786",
            "query" : {"name": "foo", "status": "open"},
            "uid" : "1416422209395",
        },
        {
            "id" : "kurfki8z6874",
            "query" : {"name": "puff", "status": "closed"},
            "uid" : "1416422209387",
        },
        {
            "id" : "kurfki8z6123",
            "query" : {"name": "joe", "status": "closed"},
            "uid" : "1416422209301",
        }
      ];
    

    这里: http://jsfiddle.net/nk5Loxoy/

    注意:如果您在查询中像编辑前一样使用数组,则应使用自定义过滤器

    【讨论】:

    • @Micheal Zucchetta:抱歉,打错了。谢谢。
    • @Micheal Zucchetta:我不明白为什么它在我的情况下不起作用。我基本上使用带有.populate('query') 的Mongoose 从MongoDB 填充query 对象。当我尝试使用相同的方法过滤您在 jsfiddle 中显示的方式时,我没有看到任何错误,但我没有看到任何输出,但我也没有看到任何输出。
    • @Micheal Zucchetta:我必须先搞定boofer in boofers | filter: 'closed',才能让事情顺利进行。谢谢。
    【解决方案2】:

    你可以使用 ng-if 吗?

    <div ng-repeat="boofer in boofers" ng-if="boofer.query.status==='closed'">{{boofer.uid}}</div>
    

    【讨论】:

    • 对不起,我错误地将嵌套的 query 对象作为一个数组,而它只是一个对象。能否请您再看一遍帖子并回答。谢谢。
    • filter: {boofer.query.status: 'closed'} 不适用于更新后的帖子。我没有收到任何错误,但也没有打印出{{boofer.uid}}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多