3

I have a custom filter function I'm calling for ng-repeat directive:

<div ng-repeat="app in appVm.appList | filter:appVm.assetFilter">{{app.title}}</div>

This obviously hits my assetFilter function for each app in the appList. When filtering is complete, I want to run another function.

How can I detect when the filtering pass has completed?

Update

Based on responses pointing me towards other answers, I can clarify a bit further.

When I implement that solution with a custom directive and using the scope.$last, I observe the following:

  • It works as expected when the page is first rendered and the list is populated - the last item is detected.
  • When my filter function is triggered and items are removed from the list, the directive handler is not hit at all.
  • When the same items are the added back into the list, but they're not the last items in the list, the directive is hit, but only for the items being added back in, and there is no item with a scope.$last of true.

Hope this helps to clarify.

Community
  • 1
  • 1
LJW
  • 2,378
  • 1
  • 21
  • 35
  • I don't think it is - see update. – LJW Nov 28 '13 at 15:24
  • Hi there, I'm stucked in the same problem, i need to call "$scope.$broadcast('masonry.reload');" after filter has ended. Did you find any way to do that? Thanks in advance – teone Apr 16 '14 at 12:51
  • This would be so easier if $filter just returned a promise object. Then you could just do... `startLoader(); $filter('filterName')(filterValue).then(function() { endLoader(); });` – Gaui May 14 '15 at 14:24

1 Answers1

1

ng-repeat creates a scope for each repeated element. This scope includes $first and $last so you can add a directive that checks for these

HTML

<div ng-repeat="item in items"  check-last>

Directive:

app.directive('checkLast',function(){
  return{
    restrict:'A',
    link:function(scope,elem,attrs){
      if(scope.$last){
            /* repeater done code*/
      }
  }
  } 
})

DEMO

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Thanks, but see update to the question which explains why this doesn't work. – LJW Nov 28 '13 at 15:23
  • well it goes to point of creating proper questions in SO in the first place. You should have put that in your question to begin with. Questions should include what you've tried and problems encountered. Create a demo – charlietfl Nov 28 '13 at 15:28
  • also what is objective of code being run. Might have to broadcast an event from filter – charlietfl Nov 28 '13 at 15:38
  • I only discovered this after running yours and others samples. The objective of the code is to run some visual jquery animations on the list container after the filtering has completed. – LJW Nov 28 '13 at 16:43
  • can't you use ng-animate or ng-class? Also I have another idea....create demo with a filter based on my demo – charlietfl Nov 28 '13 at 16:55