1

I am getting data from a server and then depending on the data I am changing a filter for ng-repeat. After I change the filter I am trying to scroll to a new element that will be visible after the ng-repeat filter has been updated.

The problem is that

I can change the filter dynamically but it takes around 5 seconds for angular to detect the change and update the ng-repeat. My scroll code runs immediately after the ng-repeat filter has changed, since it hasn't been updated, I get an error saying

the element does not exist.

My Question is

How can I make the ng-repeat update as soon as the filter is changed and then call my scroll when the ng-repeat has finished filtering?

Here is my current code

//Set ng-repeat filter
orderTypeFilter = 0;
//Scroll to element that will be visible after ng-repeat finishes updating
var topPos = document.getElementById('cardOrder0').offsetTop;
document.getElementById('main-md-content').scrollTop = topPos - 10;
Community
  • 1
  • 1
Gary Holiday
  • 3,297
  • 3
  • 31
  • 72
  • You have to use [`$scope.$apply`](http://stackoverflow.com/questions/15112584/using-scope-watch-and-scope-apply-in-angularjs), i.e. `$scope.$apply(function(){orderTypeFilter = 0;});` – fracz Jul 13 '16 at 22:05
  • Inject `$timeout` to where you're trying to `scrollTop` and put it inside `$timeout(function() { var topPos = document.getElementById.....});` – Alon Eitan Jul 13 '16 at 22:16
  • I should have added that I am doing this withing a `$mdDialog` controller, so `$scope.apply` does not work. @AlonEitan wouldn't the user have to wait 5 seconds for the `ng-repeat` to change? Is the any way to force the change instead of waiting for it to detect it? – Gary Holiday Jul 13 '16 at 22:22
  • @GaryJohnson Not when it's inside `$timeout` because it trigger a digest cycle, update the view, and only then attempt to scroll – Alon Eitan Jul 13 '16 at 22:24
  • @AlonEitan adding the `$timeout` works, but the `ng-repeat` still takes around 5 seconds to update. – Gary Holiday Jul 13 '16 at 22:32
  • @GaryJohnson In that case you need to show us how you update the array of the `ngRepeat` - Something isn't right there (Maybe you're still using jQuery's `$.ajax`??) – Alon Eitan Jul 13 '16 at 22:35

0 Answers0