1

I have an array of the following json object:

 { 
    fooBar:{ 
        Id: 2
        Foo:  { 
                Id: 4
                IsNew: false
              } 
         Bar: { 
                Id: 1
                IsNew: false
              }
           }
  }

and I want to filter on the fooBar.Foo.Id, so based on this answer I was expecting the following to work:

<div data-ng-repeat="fooBar in fooBars | filter:{Foo:{Id : 4}}:true">

But this does not return me anything.

I can do the following however to filter on fooBar.Id and that works fine:

<div data-ng-repeat="fooBar in fooBars | filter:{Id : 2}:true">

Should I be writing custom filters for this kind of one level down child property matching? Or is there anything obvious that I am doing wrong?

2
  • can you post a plunker or a fiddle? and replicate the problem? Commented Jul 30, 2014 at 8:09
  • Did you see the updated JS Bin on your referred question? Look at this filter. They ended up with another solution as well. Commented Jul 30, 2014 at 8:12

1 Answer 1

0

Based on this answer, I solved the problem by creating a custom filter:

app.filter('fooBarFilter', [function () {
 return function (foobars, fooId) {
     var result = {};

     angular.forEach(foobars, function (foobar, key) {
         if (foobar.foo.Id === fooId) {
             result[key] = foobar;
         }
     });
     return result;
 };
}]);

and used it to filter the collection in the HTML:

 <div data-ng-repeat="(key, fooBar) in fooBars | fooBarFilter : 4">
Sign up to request clarification or add additional context in comments.

Comments

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.