Skip to content

Commit ceb9d1e

Browse files
committed
fix(carousel): cancel goNext on scope destruction
Discovered after angular-ui#1451
1 parent 0b39994 commit ceb9d1e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/carousel/carousel.js

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
1414
currentTimeout, isPlaying;
1515
self.currentSlide = null;
1616

17+
var destroyed = false;
1718
/* direction: "prev" or "next" */
1819
self.select = function(nextSlide, direction) {
1920
var nextIndex = slides.indexOf(nextSlide);
@@ -31,6 +32,8 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
3132
}
3233
}
3334
function goNext() {
35+
// Scope has been destroyed, stop here.
36+
if (destroyed) { return; }
3437
//If we have a slide to transition from and we have a transition type and we're allowed, go
3538
if (self.currentSlide && angular.isString(direction) && !$scope.noTransition && nextSlide.$element) {
3639
//We shouldn't do class manip in here, but it's the same weird thing bootstrap does. need to fix sometime
@@ -66,6 +69,9 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
6669
$scope.$currentTransition = null;
6770
}
6871
};
72+
$scope.$on('$destroy', function () {
73+
destroyed = true;
74+
});
6975

7076
/* Allow outside people to call indexOf on slides array */
7177
self.indexOfSlide = function(slide) {

src/carousel/test/carousel.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ describe('carousel', function() {
234234
next.click();
235235
testSlideActive(1);
236236
});
237+
238+
it('issue 1414 - should not continue running timers after scope is destroyed', function() {
239+
testSlideActive(0);
240+
$timeout.flush();
241+
testSlideActive(1);
242+
$timeout.flush();
243+
testSlideActive(2);
244+
$timeout.flush();
245+
testSlideActive(0);
246+
scope.$destroy();
247+
expect($timeout.flush).toThrow('No deferred tasks to be flushed');
248+
});
249+
237250
});
238251

239252
describe('controller', function() {

0 commit comments

Comments
 (0)