【问题标题】:AngularJS filter array in array from json data来自json数据的数组中的AngularJS过滤器数组
【发布时间】:2016-11-08 15:31:54
【问题描述】:

我从 JSON 调用的数据非常简单。

我想过滤事件 id 为 13 的所有数据

我的 json 来自 [...] $scope.things = things

[
    {
     "id":"1",
     "title":"title1",
     "events":[
               {"id":"11",
                "events11"}, 
               {"id":"12",
                "events12"}
               ]
   },{
    "id":"2",
    "title":"title2",
    "events":[
              {"id":"11",
               "events11"}, 
              {"id":"13",
               "events13"}
             ]
  }
]

我尝试显示:

 <ion-item collection-repeat="thing in things | filter:{events.id:'13'}">
    {{thing.id}}
</ion-item>

【问题讨论】:

    标签: javascript angularjs ionic-framework angularjs-ng-repeat angularjs-filter


    【解决方案1】:

    您需要更正 JSON 格式的第一件事,例如 events11events12events13 应该是键值对,例如 "events": "11"、"events": "12" 和 "events": "13"

    然后你可以像下面这样使用深度过滤器。

    标记

    <ion-item collection-repeat="thing in things | filter:{events: {id:'13'}}">
        {{thing.id}}
    </ion-item>
    

    Plunkr here

    【讨论】:

    • @JLRishe 因为在 json 中没有改变,有问题的 JSON 是无效的,你可以检查附加的 plunkr,虽然谢谢你的提醒,干杯:)
    • 我已经修复了损坏的 JSON,但它一定无法正常工作,因为我的小提琴有 Angular 1.0.1。我想深度过滤功能是在更高版本的 Angular 中添加的。
    • 是的,如果我没记错的话,它是从 1.3+ 开始添加的
    • 感谢@PankajParkar:我不想为 id {id:'13'} 设置一个整数,而是想要一个动态值 {events.id'} 怎么做? (每个 events.id 有一个页面)
    • 我不太明白。
    【解决方案2】:

    实例化范围变量时,您可以直接在控制器中进行过滤:

    $scope.things = things.filter(obj => obj.id==='13');
    

    【讨论】:

      【解决方案3】:

      你可以尝试使用 ng-if 和辅助函数:

       <ion-item collection-repeat="thing in things" ng-if="getEventInThing(thing, '13')">
          {{thing.id}}
      </ion-item>
      

      以下函数将返回具有给定 id 的事件(如果存在):

      $scope.getEventInThing = function(thing, eventId) {
       return thing.events.find(function(event) {
        return event.id===eventId;
       });
      }
      

      【讨论】:

        【解决方案4】:
         <ion-item collection-repeat="thing in things">
            <span ng-repeat="event in thing.events" ng-if="event.id='13'"
                {{thing.id}}
         </ion-item>
        

        基本上两个带有 ng-if 的循环用于内部循环来检查偶数的 id 是否为 13。

        【讨论】:

        • item.id == '13' 是什么意思?
        • 我的错,只是错字,现在改了
        猜你喜欢
        • 1970-01-01
        • 2016-07-09
        • 1970-01-01
        • 2019-05-11
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 2018-12-24
        • 2016-10-22
        相关资源
        最近更新 更多