Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c20d438

Browse files
fix(ngSwitch): interoperate with multi-element transclude directives
Worked with @lgalfaso on the implementation here. Closes #8235 Closes #8244
1 parent 4f32e3e commit c20d438

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/ng/directive/ngSwitch.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ var ngSwitchDirective = ['$animate', function($animate) {
152152
previousElements.length = 0;
153153

154154
for (i = 0, ii = selectedScopes.length; i < ii; ++i) {
155-
var selected = selectedElements[i];
155+
var selected = getBlockElements(selectedElements[i].clone);
156156
selectedScopes[i].$destroy();
157157
previousElements[i] = selected;
158158
$animate.leave(selected, function() {
@@ -169,8 +169,10 @@ var ngSwitchDirective = ['$animate', function($animate) {
169169
selectedTransclude.transclude(function(caseElement, selectedScope) {
170170
selectedScopes.push(selectedScope);
171171
var anchor = selectedTransclude.element;
172+
caseElement[caseElement.length++] = document.createComment(' end ngSwitchWhen: ');
173+
var block = { clone: caseElement };
172174

173-
selectedElements.push(caseElement);
175+
selectedElements.push(block);
174176
$animate.enter(caseElement, anchor.parent(), anchor);
175177
});
176178
});

test/ng/directive/ngSwitchSpec.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -251,25 +251,28 @@ describe('ngSwitch', function() {
251251
}));
252252

253253

254-
it("should use the correct transclusion scope if there is a transclude directive on the ng-swith-when/ng-switch-default element", inject(function($rootScope, $compile) {
254+
it("should interoperate with other transclusion directives like ngRepeat", inject(function($rootScope, $compile) {
255255
element = $compile(
256-
'<div ng-switch="foo">' +
257-
'<pre ng-switch-when="item1" ng-repeat="bar in bars">' +
258-
'foo = {{foo}}' +
259-
'bar = {{bar}}' +
260-
'$index = {{$index}}' +
261-
'</pre>' +
256+
'<div ng-switch="value">' +
257+
'<div ng-switch-when="foo" ng-repeat="foo in foos">{{value}}:{{foo}}|</div>' +
258+
'<div ng-switch-default ng-repeat="bar in bars">{{value}}:{{bar}}|</div>' +
262259
'</div>'
263260
)($rootScope);
264-
$rootScope.$apply('foo="item1";bars=["one", "two"]');
265-
expect(element.text()).toEqual(
266-
'foo = item1' +
267-
'bar = one' +
268-
'$index = 0' +
269-
'foo = item1' +
270-
'bar = two' +
271-
'$index = 1'
272-
);
261+
$rootScope.$apply('value="foo";foos=["one", "two"]');
262+
expect(element.text()).toEqual('foo:one|foo:two|');
263+
264+
$rootScope.$apply('value="foo";foos=["one"]');
265+
expect(element.text()).toEqual('foo:one|');
266+
267+
$rootScope.$apply('value="foo";foos=["one","two","three"]');
268+
expect(element.text()).toEqual('foo:one|foo:two|foo:three|');
269+
270+
$rootScope.$apply('value="bar";bars=["up", "down"]');
271+
expect(element.text()).toEqual('bar:up|bar:down|');
272+
273+
$rootScope.$apply('value="bar";bars=["up", "down", "forwards", "backwards"]');
274+
expect(element.text()).toEqual('bar:up|bar:down|bar:forwards|bar:backwards|');
275+
273276
}));
274277

275278

0 commit comments

Comments
 (0)