diff --git a/src/ng/directive/ngSwitch.js b/src/ng/directive/ngSwitch.js index 312cdf728b0f..e34b89b0dafb 100644 --- a/src/ng/directive/ngSwitch.js +++ b/src/ng/directive/ngSwitch.js @@ -138,37 +138,29 @@ var ngSwitchDirective = ['$animate', function($animate) { }], link: function(scope, element, attr, ngSwitchController) { var watchExpr = attr.ngSwitch || attr.on, - selectedTranscludes, - selectedElements, - previousElements, + selectedTranscludes = [], + selectedElements = [], + previousElements = [], selectedScopes = []; scope.$watch(watchExpr, function ngSwitchWatchAction(value) { - var i, ii = selectedScopes.length; - if(ii > 0) { - if(previousElements) { - for (i = 0; i < ii; i++) { - previousElements[i].remove(); - } - previousElements = null; - } + var i, ii; + for (i = 0, ii = previousElements.length; i < ii; ++i) { + previousElements[i].remove(); + } + previousElements.length = 0; - previousElements = []; - for (i= 0; i' + + '

Block1

' + + '

Block2

' + + 'a' + + '' + )($rootScope); + + $rootScope.$apply('mode = "a"'); + expect(element.children().length).toBe(2); + + $rootScope.$apply('mode = "b"'); + expect(element.children().length).toBe(1); + + $rootScope.$apply('mode = "a"'); + expect(element.children().length).toBe(2); + + $rootScope.$apply('mode = "b"'); + expect(element.children().length).toBe(1); + })); }); describe('ngSwitch animations', function() {