David M. Karr
David M. Karr

Reputation: 15235

ngRepeat through entries in map and filter entries

I have an ngRepeat on a Map that gives me a "(key, value)" pair for each iteration. I now need to write a filter on this to limit some of the results I get back. I defined my filter to have two parameters. I pass one parameter in the ngRepeat call, so I should expect to get two parameters. The parameter I manually pass is a boolean. In the debugger both of those parameters were set, and the second parameter was "true", which is what I was passing, so I would expect the first parameter to correspond to my "(key, value)" pair. What I got for the first parameter was an essentially empty Object.

The reference in my HTML looks like this:

<div ng-repeat="(name, thing) in thingMap | limitThings:showOnlyFailed">

The "thingMap" is a map keyed by a "name" property, whose values are "thing" objects.

Here's my filter definition, representing what I expected to see in that first parameter:

thingModule.filter("limitThings", ['ThingsService', function(ThingsService) {
return function(entry, showOnlyFailed) {
    return !showOnlyFailed || ThingsService.anyFailuresInList(entry.thing);
};
}]);

When I get into the debugger, the value of "entry" is just "Object {}".

Upvotes: 0

Views: 121

Answers (2)

Craig Squire
Craig Squire

Reputation: 2141

I think what just-boris is saying is correct, but I modified his example to make it a little clearer. When you use ng-repeat with an object, then the whole object is passed into the filter, not each entry.

I did some filtering with angular.forEach based on a 'failed' property in the value of the object.

http://plnkr.co/edit/B2WOiSm2vZiQBKS1wlmJ?p=preview

app.filter('limitThings', function() {
  return function(entry, showOnlyFailed) {
    console.log("Entry: " + entry + " Only Failed: " + showOnlyFailed);
    var filteredMap = {};
    angular.forEach(entry, function(value, key)
    {
      if(!showOnlyFailed || !value.failed)
      {
        filteredMap[key] = value;
      }
    });
    return filteredMap;
  }
}); 

Upvotes: 0

just-boris
just-boris

Reputation: 9766

You see thingMap as argument named entry. If you got an empty object, then you thing map is empty.

I've created a small plunk http://plnkr.co/edit/miipnB?p=preview where you can see that hash map of your things are passed corectly.

Upvotes: 1

Related Questions