如何通过父 ID 过滤嵌套的 ng-repeat?
How to filter nested ng-repeat by parent id?
我有一个数组
lists = [{"id":"1234"},{"id":"5423"},{"id":"65342"}]
和包含内容的数组:
contents = [{"id":"1","listId":"1234"},{"id":"2","listId":"5423"}]
如何在 "lists" 的 ng-repeat 中过滤 "contents" 的 ng-repeat 并通过 content.listID 等于 list.id 过滤 "contents"?
ng-repeat="e in contents | filter: {listId: lists.id}"
您必须将父 ID 设置为子数组,子数组将针对该父 ID 进行过滤。 plnk 类似的例子。
此致。
您可能应该在控制器而不是视图中执行此映射,但下面的示例将向您展示如何执行此操作。
(function() {
'use strict';
angular
.module('exampleApp', [])
.controller('ExampleController', ExampleController);
function ExampleController() {
var vm = this;
vm.lists = [{
"id": "1234"
}, {
"id": "5423"
}, {
"id": "65342"
}]
vm.contents = [{
"id": "1",
"listId": "1234"
}, {
"id": "2",
"listId": "5423"
}]
}
ExampleController.$inject = [];
})();
<!DOCTYPE html>
<html ng-app='exampleApp'>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
</head>
<body ng-controller="ExampleController as vm">
<div ng-repeat="item in vm.lists">
<p ng-repeat="content in vm.contents | filter:{ listId: item.id }">Content ID: {{content.id}} Lists ID: {{item.id}}</p>
</div>
</body>
</html>
我有一个数组
lists = [{"id":"1234"},{"id":"5423"},{"id":"65342"}]
和包含内容的数组:
contents = [{"id":"1","listId":"1234"},{"id":"2","listId":"5423"}]
如何在 "lists" 的 ng-repeat 中过滤 "contents" 的 ng-repeat 并通过 content.listID 等于 list.id 过滤 "contents"?
ng-repeat="e in contents | filter: {listId: lists.id}"
您必须将父 ID 设置为子数组,子数组将针对该父 ID 进行过滤。 plnk 类似的例子。
此致。
您可能应该在控制器而不是视图中执行此映射,但下面的示例将向您展示如何执行此操作。
(function() {
'use strict';
angular
.module('exampleApp', [])
.controller('ExampleController', ExampleController);
function ExampleController() {
var vm = this;
vm.lists = [{
"id": "1234"
}, {
"id": "5423"
}, {
"id": "65342"
}]
vm.contents = [{
"id": "1",
"listId": "1234"
}, {
"id": "2",
"listId": "5423"
}]
}
ExampleController.$inject = [];
})();
<!DOCTYPE html>
<html ng-app='exampleApp'>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
</head>
<body ng-controller="ExampleController as vm">
<div ng-repeat="item in vm.lists">
<p ng-repeat="content in vm.contents | filter:{ listId: item.id }">Content ID: {{content.id}} Lists ID: {{item.id}}</p>
</div>
</body>
</html>