0xc0de
0xc0de

Reputation: 8297

Applying map function on an array after filter

In my html (.ng-template) I have an array, on which a filter is applied. I want to apply a map function to the result of this filter function (It's too expensive to apply it to the whole array before applying the filter). The older versions of angularjs allowed me to do so by

<ng-repeat = 'item in array.$filter(filterFunc).map(mapFunc).$orderBy("orderFunc()")'>

But with the new syntax (v 1.0.5) I am not able to do this.

<ng-repeat = 'item in array|filter:filterFunc|orderBy:"orderFunc()"'>

I do don't understand how do I apply map function to this. Is there any way to accomplish this?

Upvotes: 0

Views: 215

Answers (1)

Umur Kontacı
Umur Kontacı

Reputation: 35478

Templates are hard to test, do not write any business logic in the templates, besides, these functions will create a new copy of your array and apply map function every time in every $digest phase.

You should do this in your controller and use ngRepeat within that.

function MyCtrl ($scope, $filter) {
  var fullArray = [1,2,3];
  var otherArray = $filter(filterFunc).map(mapFunc);
}

.

<div ng-repeat="item in otherArray | orderBy:orderFn"></div>

You can also order the array in the controller.

Upvotes: 1

Related Questions