angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Using the same array of objects in multiple places within HTML calls a custom filter multiple times though the filter is applied only once.

Open sjselvan opened this issue 12 years ago • 3 comments

JS:

function MyController($scope){
   $scope.addresses = [{obj1},{obj2},{obj3}];
 }

HTML

<html>
<body>
<ul id="one">
<li ng-repeat="address in addressess ">{{address.name}}</li>
</ul>
<ul id="two">
<li ng-repeat="address in addressess ">{{address.name}}</li>
</ul>
<ul id="three">
  <li ng-repeat="address in addressess | customFilter">{{address.name}}</li>
</ul>
</body>
</html>
  1. I have a controller with a property assigned an array of objects.
  2. I'm using the same object in the html in multiple places.
  3. In one of the places in the html, i apply a filter to show only specific set of values.
  4. The filter code gets called for all the iterations used in the html page.
    • In the above scenario the customFilter got called thrice as i've used the addresses collection in three different places within the html but the filter is called only once under the element id 'three'

sjselvan avatar May 09 '14 00:05 sjselvan

Hi, A filter should be a pure function that may end up being called multiple times, that said, in this specific case I would expect that the filter gets called twice and in all cases only for the last ng-repeat. Can you please create a plunker that shows this issue?

lgalfaso avatar May 09 '14 14:05 lgalfaso

@sjselvan I agree with @lgalfaso , putting your code in plunker shows exactly 2 calls to a custom filter, as expected: http://plnkr.co/edit/SZuSXZLRwRvasGyE72XZ?p=preview

I guess something else is going in your code but it is impossible to say more without a minimal, live reproduce scenario. I'm going to close this issue for now as it is not actionable as-is. I would be happy to re-open if you've got more info.

pkozlowski-opensource avatar May 20 '14 17:05 pkozlowski-opensource

i'm unable to replicate the issue. will update you folks, if i see it again.

sjselvan avatar May 21 '14 01:05 sjselvan