2016-09-05 317 views
0

键我有这样的:过滤NG重复与父NG-重复

<div class="row" ng-repeat="(key, value) in categories"> 
    <h5>{{ value }}</h5> 
    <ul> 
     <li ng-repeat="post in $parent.posts | filter: key">{{ post.name }} -- {{ post.category }}</li> 
    </ul> 
</div> 

我想要的职位的嵌套ng-repeat是由key的类别进行筛选。

的想法是让每个岗位显示相关的帖子,其中{{ post.category }}是相同{{ value }}

+0

[在角度如何可以遍历键,在值NG-重复](的可能的复制http://stackoverflow.com/questions/15127834/how-can-i -ite-over-the-keys-value-in-ng-repeat-in-angular) –

+0

@DurgpalSingh不,不重复。我的问题是,如何使用第一个ng-repeat中的'key'作为第二个ng-repeat的过滤器。你给的链接是如何在数据集中执行'(key,value)' – Rexford

回答

2

这一要求遍历object并使用key of parent过滤孩子ng-repeat可以通过使用custom function来实现这反过来返回对象。检查下面创建的示例应用程序。

var app = angular.module('sample', []); 
 

 
app.controller('samplecontroller', function($scope) { 
 

 
    $scope.filterKey = function(value) { 
 
    return { 
 
     name: value 
 
    }; 
 
    }; 
 

 
    $scope.categories = { 
 
    'John': 'English' 
 
    }; 
 
    $scope.posts = [{ 
 
    name: 'John', 
 
    category: 'General' 
 
    }, { 
 
    name: 'James', 
 
    category: 'Restricted' 
 
    }] 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app="sample"> 
 
    <div ng-controller="samplecontroller"> 
 
    <div class="row" ng-repeat="(key, value) in categories"> 
 
     <h5>{{ value }}</h5> 
 
     <ul> 
 
     <li ng-repeat="post in $parent.posts | filter: filterKey(key)">{{ post.name }} -- {{ post.category }}</li> 
 
     </ul> 
 
    </div> 
 
    </div> 
 

 
</body>

+0

对于我的用例(与$ firebaseArray'有关),我最终做了'return value;'而不是'return {name:value}'谢谢 – Rexford