Detect when a custom filter has completed in AngularJS [duplicate]

拟墨画扇 提交于 2019-12-22 09:40:23

问题


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:


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



来源:https://stackoverflow.com/questions/20268833/detect-when-a-custom-filter-has-completed-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!