簡體   English   中英

使用父ng-repeat的密鑰過濾ng-repeat

[英]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 functioncustom 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM