[英]Filter ng-repeat with key from parent ng-repeat
我有這個:
<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
通過category key
進行過濾。
想法是讓每個帖子都顯示相關的帖子,其中{{ post.category }}
與{{ value }}
可以通過使用custom function
該custom function
依次返回對象)來實現遍歷object
並使用key of parent
過濾子ng-repeat
的要求。 檢查下面創建的示例應用程序。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.