Skip to content

Commit 75bb641

Browse files
committed
fix(ngAnimate): remove event listeners only after all listeners have been called
The fix for removing the event callbacks on destroy introduced in ce7f400 removed the events too early, so that the event callbacks for the "close" phase in "leave" animations were not called. This commit fixes the behavior so that the event callbacks are only removed during on('$destroy') when no animation is currently active on the element. When an animation is active, the event callbacks will be removed after all callbacks have run, and if the element has no parent (has been removed from the DOM). Closes angular#14321
1 parent cf3f95d commit 75bb641

File tree

2 files changed

+248
-114
lines changed

2 files changed

+248
-114
lines changed

src/ngAnimate/animateQueue.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
208208
});
209209
}
210210

211+
function cleanupEventListeners(phase, element) {
212+
if (phase === 'close' && !element[0].parentNode) {
213+
// If the element is not attached to a parentNode, it has been removed by
214+
// the domOperation, and we can safely remove the event callbacks
215+
$animate.off(element);
216+
}
217+
}
218+
211219
var $animate = {
212220
on: function(event, container, callback) {
213221
var node = extractElementNode(container);
@@ -219,7 +227,14 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
219227

220228
// Remove the callback when the element is removed from the DOM
221229
jqLite(container).on('$destroy', function() {
222-
$animate.off(event, container, callback);
230+
var animationDetails = activeAnimationsLookup.get(node);
231+
232+
if (!animationDetails) {
233+
// If there's an animation ongoing, the callback calling code will remove
234+
// the event listeners. If we'd remove here, the callbacks would be removed
235+
// before the animation ends
236+
$animate.off(event, container, callback);
237+
}
223238
});
224239
},
225240

@@ -547,7 +562,10 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
547562
forEach(callbacks, function(callback) {
548563
callback(element, phase, data);
549564
});
565+
cleanupEventListeners(phase, element);
550566
});
567+
} else {
568+
cleanupEventListeners(phase, element);
551569
}
552570
});
553571
runner.progress(event, phase, data);

0 commit comments

Comments
 (0)