ng-repeat数组在数组中使用AngularJS的父属性进行过滤

时间:2015-12-11 21:40:44

标签: angularjs multidimensional-array angularjs-ng-repeat ng-repeat

我想在父类的属性中选择数组。我不知道索引(在这种情况下是1),但我知道id。我可以做吗?这是我的索引工作示例,我想通过id(43)获得相同的结果。

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div data-ng-app="myApp" data-ng-controller="myCtrl">
 <ul>
    <li ng-repeat="p in people[1].rooms" ng-bind="p"></li>
 </ul>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.people = [
    {
        id: 25,
        nick: 'Joker',
        rooms: ['room1', 'room2']
    },
    {
        id: 43,
        nick: 'King',
        rooms: ['room3', 'room4']
    },
    {
        id: 78,
        nick: 'Queen',
        rooms: ['room5', 'room6']
    }
 ]
});
</script>

2 个答案:

答案 0 :(得分:2)

您应该使用people数组上的严格过滤器,按特定用户为特定用户分隔房间。显而易见的是,您将基于id的搜索进行单个记录。

<强>代码

<ul>
    <li ng-repeat="p in filteredPeople = (people | filter: {id: l.id })[0].rooms">
       {{p}}
    </li>
</ul>

答案 1 :(得分:2)

这是一个选择:

<ul ng-repeat="p in people | filter: { id: 45 }">
    <li ng-repeat="room in p.rooms" ng-bind="room"></li>
</ul>