Detect when a custom filter has completed in Angul

2019-05-10 07:58发布

This question already has an answer here:

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.

1条回答
我命由我不由天
2楼-- · 2019-05-10 08:04

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

查看更多
登录 后发表回答