2

I hope someone can help me. For my current project in angular 1.3 I'm using this list:

$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.

I used this 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...

Has anyone any ideas?

Edit

I FINALLY found out what caused the problem, I was using angular 1.3.0. After upgrading to 1.3.11 it worked!!

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried your suggestion, but it isn't working because 'content' contains an array of objects. If the object 'content' had a property 'id' your are totally right...
@user2246413, works fine for me with your (slightly modified for illustration) data: plnkr.co/edit/katQANxYUVmWvu8yeltU?p=preview
@user2246413, did you try it with how I showed in the plunker?
I FINALLY found out what caused the problem, I was using angular 1.3.0. After upgrading to 1.3.11 it worked!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.