简体   繁体   English

Angular 1.3带有数组属性的对象的过滤器列表

[英]Angular 1.3 filter list of objects with array attribute

I hope someone can help me. 我希望有一个人可以帮助我。 For my current project in angular 1.3 I'm using this list: 对于我当前在angular 1.3中的项目,我使用以下列表:

$scope.myList = [{
  id: "obj1",
  content: [{
    id: 1,
    name: 'attr 1'
  }, {
    id: 2,
    name: 'attr 2'
  }, {
    id: 3,
    name: 'attr 3'
  }]
}, {
  id: "obj2",
  content: [{
    id: 4,
    name: 'attr 4'
  }, {
    id: 5,
    name: 'attr 5'
  }, {
    id: 6,
    name: 'attr 6'
  }]
}, {
  id: "obj3",
  content: [{
    id: 7,
    name: 'attr 7'
  }, {
    id: 8,
    name: 'attr 8'
  }, {
    id: 9,
    name: 'attr 9'
  }]
}];

I would like to get the object which has the id X in the content array. 我想获得内容数组中ID为X的对象。

I used this ng-repeat: 我使用了此ng-repeat:

<ul>
  <li ng-repeat="item in myList | filter: {content: [{id:1}]}">
    {{item}}
  </li>
</ul>

When I use id:1, id:4 or id:7 it works, but not for the other ids... 当我使用id:1,id:4或id:7时,它可以工作,但不适用于其他id ...

Has anyone any ideas? 有任何想法吗?

Edit 编辑

I FINALLY found out what caused the problem, I was using angular 1.3.0. 我最终发现了问题的原因,我使用的是angular 1.3.0。 After upgrading to 1.3.11 it worked!! 升级到1.3.11后,它可以正常工作!!

You can filter based on nested properties like so: 您可以根据嵌套属性进行过滤,如下所示:

<li ng-repeat="item in myList | filter: {content: {id: '1'}}">
   {{item}}
</li>

It's important to note that the "object" (that has the id X) you'll get will be at the item level. 重要的是要注意,您将获得的“对象”(具有ID X)将在item级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM