使用来自父ng-repeat的键过滤ng-repeat

时间:2016-09-05 12:51:41

标签: javascript angularjs

我有这个:

<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 }}相同

1 个答案:

答案 0 :(得分:2)

可以使用object来实现迭代ng-repeat并使用key of parent过滤子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>
{{1}}

相关问题