Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9257674

Browse files
committed
fix($animate): wait two until two digests are over until enabling animations
Even when no remote templates are to be downloaded, wait until the end of the post digest queue before enabling animations since all $animate-triggered animation events perform a post digest before running animations. Closes #8844
1 parent c9b0bfe commit 9257674

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/ngAnimate/animate.js

+18-21
Original file line numberDiff line numberDiff line change
@@ -411,34 +411,31 @@ angular.module('ngAnimate', ['ng'])
411411

412412
$rootElement.data(NG_ANIMATE_STATE, rootAnimateState);
413413

414-
// disable animations during bootstrap, but once we bootstrapped, wait again
415-
// for another digest until enabling animations. Enter, leave and move require
416-
// a follow-up digest so having a watcher here is enough to let both digests pass.
417-
// However, when any directive or view templates are downloaded then we need to
418-
// handle postpone enabling animations until they are fully completed and then...
419-
var watchFn = $rootScope.$watch(
414+
// Wait until all directive and route-related templates are downloaded and
415+
// compiled. The $templateRequest.totalPendingRequests variable keeps track of
416+
// all of the remote templates being currently downloaded. If there are no
417+
// templates currently downloading then the watcher will still fire anyway.
418+
var deregisterWatch = $rootScope.$watch(
420419
function() { return $templateRequest.totalPendingRequests; },
421420
function(val, oldVal) {
422-
if (oldVal === 0) {
423-
if (val === 0) {
424-
$rootScope.$$postDigest(onApplicationReady);
425-
}
426-
} else if(val === 0) {
427-
// ...when the template has been downloaded we digest twice again until the
428-
// animations are set to enabled (since enter, leave and move require a
429-
// follow-up).
421+
if (val !== 0) return;
422+
deregisterWatch();
423+
424+
// Now that all templates have been downloaded, $animate will wait until
425+
// the post digest queue is empty before enabling animations. By having two
426+
// calls to $postDigest calls we can ensure that the flag is enabled at the
427+
// very end of the post digest queue. Since all of the animations in $animate
428+
// use $postDigest, it's important that the code below executes at the end.
429+
// This basically means that the page is fully downloaded and compiled before
430+
// any animations are triggered.
431+
$rootScope.$$postDigest(function() {
430432
$rootScope.$$postDigest(function() {
431-
$rootScope.$$postDigest(onApplicationReady);
433+
rootAnimateState.running = false;
432434
});
433-
}
435+
});
434436
}
435437
);
436438

437-
function onApplicationReady() {
438-
rootAnimateState.running = false;
439-
watchFn();
440-
}
441-
442439
var globalAnimationCounter = 0;
443440
var classNameFilter = $animateProvider.classNameFilter();
444441
var isAnimatableClassName = !classNameFilter

test/ng/directive/ngClassSpec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,10 @@ describe('ngClass animations', function() {
434434
});
435435
inject(function($compile, $rootScope, $browser, $rootElement, $animate, $timeout, $document) {
436436

437-
// Animations are enabled right away since there are no remote HTTP template requests
437+
// Animations need to digest twice in order to be enabled regardless if there are no template HTTP requests.
438+
$rootScope.$digest();
439+
digestQueue.shift()();
440+
438441
$rootScope.$digest();
439442
digestQueue.shift()();
440443

0 commit comments

Comments
 (0)