0

Abstract

Hi, I'm trying to manipulate scroll logic of my angular application (by hand), Since there is no such thing as onLoaded event in angular, I was trying to accomplish that within $interval loop. It worked but UI was flickering, it looked like the scroll first goes all way up and then down again making everything look terrible. I've started looking for solution to my problem and found this answer. In which it was said that I have to be looking to $evalAsync which is capable of queueing an operation before UI does it's rendering. It lead me to the following code:

    var restoreScroll = function (scroll) {
        angular.element($window).scrollTop(scroll);

        var fn = function () {
            debugger;
            var value = angular.element($window).scrollTop();

            if (value !== scroll) {
                angular.element($window).scrollTop(scroll);

                $rootScope.$evalAsync(fn);
            }
        }

        $rootScope.$evalAsync(fn);
    };

which hangs and shows my misunderstanding of Angular's $evalAsync method.

Question

According to code above I need a method that would be trying to set the scroll until it succeeds, after when it should stop, how do you do that using $evalAsync?

Thank you in advance!

==================

Edit

Ive managed to make it work, however not without the devils (the $$postDigest) in my code, so here goes:

    var restoreScroll = function (scroll) {
        angular.element($window).scrollTop(scroll);

        var fn = function () {
            var value = angular.element($window).scrollTop();

            if (value !== scroll) {
                angular.element($window).scrollTop(scroll);

                $rootScope.$$postDigest(fn);
            }
        }

        $rootScope.$$postDigest(fn);
    };

Is there a better way?

Community
  • 1
  • 1
Lu4
  • 14,873
  • 15
  • 79
  • 132
  • Have you tried $viewContentLoaded. more info http://guilhebl.github.io/ui/2014/04/27/angular-view-content-loaded/ – Kishor Sharma Jun 29 '15 at 17:44
  • Yup, I did, it happens earlier than needed unfortunately... – Lu4 Jun 29 '15 at 17:54
  • $$postDigest is private to angular and we should avoid using it, Instead $timeout would be better IMO. see here: http://blogs.microsoft.co.il/choroshin/2014/04/08/angularjs-postdigest-vs-timeout-when-dom-update-is-needed/ – Kishor Sharma Jun 29 '15 at 18:02
  • As was mentioned in the question $interval (i.e. $timeout) causes flickering... – Lu4 Jun 29 '15 at 18:12

0 Answers0