Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS 1:使用父对象的属性筛选子ng重复循环中的对象_Angularjs_Angularjs Directive - Fatal编程技术网

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)

  • 市场营销
  • 项目管理
  • 分析
蒂姆(身份证号码:2)

  • 大数据
  • 数据可视化
约翰(身份证号码:3)

  • 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中创建的元素数量