Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e29fc3

Browse files
committedNov 10, 2016
fixup! refactor and add test
1 parent 6eae31c commit 2e29fc3

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
 

‎src/ng/directive/ngSwitch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ var ngSwitchDirective = ['$animate', '$compile', function($animate, $compile) {
161161
scope.$watch(watchExpr, function ngSwitchWatchAction(value) {
162162
var i, ii;
163163

164-
for (i = 0, ii = previousLeaveAnimations.length; i < ii; ++i) {
165-
$animate.cancel(previousLeaveAnimations[i]);
164+
// Start with the last, in case the array is modified during the loop
165+
while(previousLeaveAnimations.length) {
166+
$animate.cancel(previousLeaveAnimations.pop());
166167
}
167-
previousLeaveAnimations.length = 0;
168168

169169
for (i = 0, ii = selectedScopes.length; i < ii; ++i) {
170170
var selected = getBlockNodes(selectedElements[i].clone);

‎test/ng/directive/ngSwitchSpec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,36 @@ describe('ngSwitch', function() {
333333
}));
334334

335335

336+
it('should handle changes to the switch value in a digest loop with multiple value matches',
337+
inject(function($$rAF, $compile, $rootScope, $timeout) {
338+
var scope = $rootScope.$new();
339+
scope.value = 'foo';
340+
341+
scope.$watch('value', () => {
342+
if (scope.value === 'bar') {
343+
scope.$evalAsync(function() {
344+
scope.value = 'baz';
345+
});
346+
}
347+
});
348+
349+
element = $compile(
350+
'<div ng-switch="value">' +
351+
'<div ng-switch-when="foo">FOO 1</div>' +
352+
'<div ng-switch-when="foo">FOO 2</div>' +
353+
'<div ng-switch-when="bar">BAR</div>' +
354+
'<div ng-switch-when="baz">BAZ</div>' +
355+
'</div>')(scope);
356+
357+
scope.$apply();
358+
expect(element.text()).toBe('FOO 1FOO 2');
359+
360+
scope.$apply('value = "bar"');
361+
expect(element.text()).toBe('BAZ');
362+
})
363+
);
364+
365+
336366
describe('ngSwitchWhen separator', function() {
337367
var $$rAF;
338368

0 commit comments

Comments
 (0)
This repository has been archived.