AngularJS 1:使用父对象的属性筛选子ng重复循环中的对象
我无法将表达式用作筛选参数 我有两个对象数组AngularJS 1:使用父对象的属性筛选子ng重复循环中的对象,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我无法将表达式用作筛选参数 我有两个对象数组 app.controller('MainCtrl', function($scope) { $scope.name = 'World'; $scope.clients = [ {'client_id' : '1', 'client_name' : 'Anne'}, {'client_id' : '2', 'client_name' : 'Tim'}, {'client_id' : '3', 'client_na
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.clients = [
{'client_id' : '1', 'client_name' : 'Anne'},
{'client_id' : '2', 'client_name' : 'Tim'},
{'client_id' : '3', 'client_name' : 'John'}
];
$scope.courses = [
{'ordered_by' : '1', 'course_name' : 'Marketing'},
{'ordered_by' : '1', 'course_name' : 'Project Management'},
{'ordered_by' : '1', 'course_name' : 'Analytics'},
{'ordered_by' : '2', 'course_name' : 'Big Data'},
{'ordered_by' : '2', 'course_name' : 'Data Visualization'},
{'ordered_by' : '3', 'course_name' : 'PHP'}
];
});
使用ng repeat指令在它们之间循环工作正常,但我希望能够使用父循环当前处理的对象的属性值来过滤嵌套循环
下面的代码列出了每个人的所有课程,因为我不知道如何将client_id值传递给嵌套指令的过滤器
<body ng-controller="MainCtrl">
<p>Course list:</p>
<div ng-repeat="person in clients">
<strong><h2>{{person.client_name}} (ID: {{person.client_id}})</h2></strong>
<ul>
<!-- I wish it was this simple:
<li ng-repeat="course in courses | filter : {'ordered_by' : '{{person.client_id}}' }"> -->
<li ng-repeat="course in courses | filter : {'ordered_by' : '' }">
{{course.course_name}}<br><span>(Ordered by person with the ID: {{course.ordered_by}})</span>
</li>
</ul>
</div>
</body>
课程表:
{{person.client\u name}(ID:{{person.client\u ID}})
-
{{course.course\u name}
(由ID为{{course.Ordered\u by}的人排序)
最终输出应如下所示:
安妮(身份证号码:1)
- 市场营销
- 项目管理
- 分析
- 大数据
- 数据可视化
- PHP
<div ng-repeat="person in clients">
<strong><h2>{{person.client_name}} (ID: {{person.client_id}})</h2></strong>
<ul>
<li ng-repeat="course in courses">
<span ng-if="person.client_id === course.ordered_by">
{{course.course_name}}
</span>
</li>
</ul>
</div>
{{person.client\u name}(ID:{{person.client\u ID}})
-
{{课程.课程名称}
不过我还没有测试过。如果在
ng repeat
中出现ng条件,请尝试使用ng:
<li ng-repeat="course in courses" ng-if="course.ordered_by == person.client_id">
...
</li>
...
.谢谢!成功了。我只对代码做了一个小的修改,使ng repeat和ng if指令位于同一个元素中(以避免为空的元素): 给出了所需的输出。非常感谢。我非常感谢您的帮助。为了提高效率,您应该将逻辑(比较)移动到您的控制器,让ng if引用您的逻辑方法。效果非常好。谢谢。我认为这是在li
中放置ng if
而不是子span
(如建议)的更好方法,因为它将减少在DOM中创建的元素数量