AngularJS 从 json 数据中过滤数组中的数组
AngularJS filter array in array from json data
我从 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>
实例化作用域变量时,可以直接在控制器中过滤:
$scope.things = things.filter(obj => obj.id==='13');
您可以尝试使用 ng-if 和辅助函数:
<ion-item collection-repeat="thing in things" ng-if="getEventInThing(thing, '13')">
{{thing.id}}
</ion-item>
以下函数将 return 具有给定 ID 的事件(如果存在):
$scope.getEventInThing = function(thing, eventId) {
return thing.events.find(function(event) {
return event.id===eventId;
});
}
你需要纠正的第一件事是 JSON 格式,例如 events11
、events12
和 events13
应该是这样的键值对 "events": "11"
, "events": "12" & "events": "13"
.
然后你可以像下面那样使用深层过滤器。
标记
<ion-item collection-repeat="thing in things | filter:{events: {id:'13'}}">
{{thing.id}}
</ion-item>
<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 来检查 even 的 id 是否为 13。
我从 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>
实例化作用域变量时,可以直接在控制器中过滤:
$scope.things = things.filter(obj => obj.id==='13');
您可以尝试使用 ng-if 和辅助函数:
<ion-item collection-repeat="thing in things" ng-if="getEventInThing(thing, '13')">
{{thing.id}}
</ion-item>
以下函数将 return 具有给定 ID 的事件(如果存在):
$scope.getEventInThing = function(thing, eventId) {
return thing.events.find(function(event) {
return event.id===eventId;
});
}
你需要纠正的第一件事是 JSON 格式,例如 events11
、events12
和 events13
应该是这样的键值对 "events": "11"
, "events": "12" & "events": "13"
.
然后你可以像下面那样使用深层过滤器。
标记
<ion-item collection-repeat="thing in things | filter:{events: {id:'13'}}">
{{thing.id}}
</ion-item>
<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 来检查 even 的 id 是否为 13。