2016-09-05 256 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