diff --git a/src/ng/directive/ngSwitch.js b/src/ng/directive/ngSwitch.js index 9abe61ca860f..1eb1d32a4e21 100644 --- a/src/ng/directive/ngSwitch.js +++ b/src/ng/directive/ngSwitch.js @@ -152,7 +152,7 @@ var ngSwitchDirective = ['$animate', function($animate) { previousElements.length = 0; for (i = 0, ii = selectedScopes.length; i < ii; ++i) { - var selected = selectedElements[i]; + var selected = getBlockElements(selectedElements[i].clone); selectedScopes[i].$destroy(); previousElements[i] = selected; $animate.leave(selected, function() { @@ -166,12 +166,13 @@ var ngSwitchDirective = ['$animate', function($animate) { if ((selectedTranscludes = ngSwitchController.cases['!' + value] || ngSwitchController.cases['?'])) { scope.$eval(attr.change); forEach(selectedTranscludes, function(selectedTransclude) { - var selectedScope = scope.$new(); - selectedScopes.push(selectedScope); - selectedTransclude.transclude(selectedScope, function(caseElement) { + selectedTransclude.transclude(function(caseElement, selectedScope) { + selectedScopes.push(selectedScope); var anchor = selectedTransclude.element; + caseElement[caseElement.length++] = document.createComment(' end ngSwitchWhen: '); + var block = { clone: caseElement }; - selectedElements.push(caseElement); + selectedElements.push(block); $animate.enter(caseElement, anchor.parent(), anchor); }); }); @@ -183,7 +184,7 @@ var ngSwitchDirective = ['$animate', function($animate) { var ngSwitchWhenDirective = ngDirective({ transclude: 'element', - priority: 800, + priority: 1200, require: '^ngSwitch', multiElement: true, link: function(scope, element, attrs, ctrl, $transclude) { @@ -194,7 +195,7 @@ var ngSwitchWhenDirective = ngDirective({ var ngSwitchDefaultDirective = ngDirective({ transclude: 'element', - priority: 800, + priority: 1200, require: '^ngSwitch', multiElement: true, link: function(scope, element, attr, ctrl, $transclude) { diff --git a/test/ng/directive/ngSwitchSpec.js b/test/ng/directive/ngSwitchSpec.js index 48e27ae9f50a..5e48d68f19f3 100644 --- a/test/ng/directive/ngSwitchSpec.js +++ b/test/ng/directive/ngSwitchSpec.js @@ -241,7 +241,7 @@ describe('ngSwitch', function() { $rootScope.url = 'x'; $rootScope.$apply(); expect(getChildScope()).toBeUndefined(); - expect(child1.$destroy).toHaveBeenCalledOnce(); + expect(child1.$destroy).toHaveBeenCalled(); $rootScope.url = 'a'; $rootScope.$apply(); @@ -251,6 +251,31 @@ describe('ngSwitch', function() { })); + it("should interoperate with other transclusion directives like ngRepeat", inject(function($rootScope, $compile) { + element = $compile( + '<div ng-switch="value">' + + '<div ng-switch-when="foo" ng-repeat="foo in foos">{{value}}:{{foo}}|</div>' + + '<div ng-switch-default ng-repeat="bar in bars">{{value}}:{{bar}}|</div>' + + '</div>' + )($rootScope); + $rootScope.$apply('value="foo";foos=["one", "two"]'); + expect(element.text()).toEqual('foo:one|foo:two|'); + + $rootScope.$apply('value="foo";foos=["one"]'); + expect(element.text()).toEqual('foo:one|'); + + $rootScope.$apply('value="foo";foos=["one","two","three"]'); + expect(element.text()).toEqual('foo:one|foo:two|foo:three|'); + + $rootScope.$apply('value="bar";bars=["up", "down"]'); + expect(element.text()).toEqual('bar:up|bar:down|'); + + $rootScope.$apply('value="bar";bars=["up", "down", "forwards", "backwards"]'); + expect(element.text()).toEqual('bar:up|bar:down|bar:forwards|bar:backwards|'); + + })); + + it('should not leak jq data when compiled but not attached to parent when parent is destroyed', inject(function($rootScope, $compile) { element = $compile(