From 66d64b332190be1277c14e3e28431d8e8e32de44 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Tue, 6 May 2014 22:57:31 -0400 Subject: [PATCH] fix(ngSwitch): properly support case labels with different numbers of transclude fns Due to a regression introduced several releases ago, the ability for multiple transclude functions to work correctly changed, as they would break if different case labels had different numbers of transclude functions. This CL corrects this by not assuming that previous elements and scope count have the same length. --- src/ng/directive/ngSwitch.js | 42 +++++++++++++------------------ test/ng/directive/ngSwitchSpec.js | 23 +++++++++++++++++ 2 files changed, 40 insertions(+), 25 deletions(-) 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() {