ng-repeat按先前ng-repeat的对象过滤

时间:2017-04-20 19:32:27

标签: angularjs angularjs-directive angularjs-ng-repeat

我有这种结构,但我不知道如何使用先前 ng-repeat 迭代的对象过滤 ng-repeat 内容。< / p>

// ############################################# ############################### //

我的模型

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

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

$scope.dashboards =[{"name":"Dashboard 1","category":"C1","subcategory":"SC1"},
                    {"name":"Dashboard 2","category":"C1","subcategory":"SC2"},
                    {"name":"Dashboard 3","category":"C2","subcategory":"SC1"},
                    {"name":"Dashboard 4","category":"C2","subcategory":"SC2"},
                    {"name":"Dashboard 5","category":"C2","subcategory":"SC3"}];
});

// ############################################# ############################### //

我的 html

<div ng-app="myApp">
<div ng-controller="controller">

<div ng-repeat="dashboard in dashboards | unique: 'category'"><!--Level 1-->
   <h2>{{dashboard.category}}</h2>
   <div ng-repeat="dashboard in dashboards | filter: { category: {{dashboard.category}} } | unique: 'subcategory'"><!--Level 2-->
      </h3>{{dashboard.subcategory}}</h3>
      <div ng-repeat="dashboard in dashboards | filter: { subcategory: {{dashboard.subcategory}} }"><!--Level 3-->
         <p>{{dashboard.name}}</p>
      </div>
   </div>
</div>

</div>
</div>

我的问题是我不知道如何使用较低级别的内容来过滤ng-repeat。

例如,我尝试使用表达式使用级别1的类别过滤级别2,使用其他表达式使用级别2的子类别过滤级别3

*在1级和2级,我使用一个过滤器,它返回我模型的唯一值

1 个答案:

答案 0 :(得分:1)

HTML存在一些相当简单的问题。

首先,{{ }}内不允许使用表达式(ng-repeat);解析器将在没有表达式语法的情况下找出属性。

&#13;
&#13;
<div ng-repeat="dashboard in dashboards | unique: 'category'">
  <!--Level 1-->
  <h2>{{dashboard.category}}</h2>
  <div ng-repeat="dashboard in dashboards | filter: { category: dashboard.category } | unique: 'subcategory'">
    <!--Level 2-->
    <h3>{{dashboard.subcategory}}</h3>
    <div ng-repeat="dashboard in dashboards | filter: { subcategory: dashboard.subcategory }">
      <!--Level 3-->
      <p>{{dashboard.name}}</p>
    </div>
  </div>
&#13;
&#13;
&#13;

http://plnkr.co/edit/dniUyHdIWGFOWYJyKjjf?p=preview

然而,这不是整个故事。 3级div需要通过 categorysubcategory进行过滤。

&#13;
&#13;
<div ng-repeat="dashboard in dashboards | unique: 'category'">
  <!--Level 1-->
  <h2>{{dashboard.category}}</h2>
  <div ng-repeat="dashboard in dashboards | filter: { category: dashboard.category } | unique: 'subcategory'">
    <!--Level 2-->
    <h3>{{dashboard.subcategory}}</h3>
    <div ng-repeat="dashboard in dashboards | filter: { category: dashboard.category, subcategory: dashboard.subcategory }">
      <!--Level 3-->
      <p>{{dashboard.name}}</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

http://plnkr.co/edit/6ClArqDH3OiQ7ECT95YD?p=preview