R
R
Rain Summers2016-09-16 21:16:50
Angular
Rain Summers, 2016-09-16 21:16:50

AngularJS. How to catch the exit event of the "ng-repeat" directive when sorting or filtering?

Hello!
I have the following code

<tr ng-repeat="row in $ctrl.rows | filter:$ctrl.filterBy | orderBy : $ctrl.order.by : $ctrl.order.reverse as results track by row.id"
      ng-init="$ctrl.afterRepeat = results.length"
  >

Through the watcher in the controller class, I catch the event of changing the "$ctrl.afterRepeat" variable, which occurs at the end of the "ng-repeat" cycle. Yes, it's a perversion, I know. :) And I also know and tried another, less perverted option through
....
  ng-init="$last && $ctrl.onRepeatEnd()"
....

The problem is that such a construction in both versions only rolls during the initial rendering of the page. During subsequent filtering, when I add filtering characters - no (i.e., the size of the repeat-array should be narrowed, and the elements removed from the DOM'a), and when I remove the filtering characters from the input field, then - yes, it works (t .e. the filtered array has increased, new elements have been added to the DOM).
In general, how to reliably catch the change event on ng-repeat?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2016-09-16
@summer_rain

From ng-repeat you can get the resulting array that is being rendered, you can try to track it.

S
Stepanya, 2016-09-19
@Stepanya

Why filter in a template? Sort array in js and return already sorted array

function ctrl() {
 var vm = this;
 var array = [2,1,3];
 vm.filteredRows= null;

  function doFilter(array) {
   //фильтруем...
   return filtered;
  }

vm.$onInit = function () {
  vm.filteredRows= doFilter(array);
}

<tr ng-repeat="row in $ctrl.filteredRows></tr>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question