Skip to content

Commit 623900e

Browse files
lgalfasojbdeboer
authored andcommitted
feat(ng:switch): Preserve the order of the elements not in the ng-switch
Preserve the order of the elements that are not part of a case nor default in a ng-switch directive Closes angular#1074
1 parent 90ba9aa commit 623900e

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/ng/directive/ngSwitch.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
* @restrict EA
77
*
88
* @description
9-
* Conditionally change the DOM structure.
9+
* Conditionally change the DOM structure. Elements within ngSwitch but without
10+
* ngSwitchWhen or ngSwitchDefault directives will be preserved at the location
11+
* as specified in the template
1012
*
1113
* @usageContent
1214
* <ANY ng-switch-when="matchValue1">...</ANY>
1315
* <ANY ng-switch-when="matchValue2">...</ANY>
1416
* ...
1517
* <ANY ng-switch-default>...</ANY>
18+
* <ANY>...</ANY>
1619
*
1720
* @scope
1821
* @param {*} ngSwitch|on expression to match against <tt>ng-switch-when</tt>.
@@ -26,6 +29,7 @@
2629
* are multiple default cases, all of them will be displayed when no other
2730
* case match.
2831
*
32+
*
2933
* @example
3034
<doc:example>
3135
<doc:source>
@@ -90,9 +94,9 @@ var ngSwitchDirective = valueFn({
9094
forEach(selectedTranscludes, function(selectedTransclude) {
9195
var selectedScope = scope.$new();
9296
selectedScopes.push(selectedScope);
93-
selectedTransclude(selectedScope, function(caseElement) {
97+
selectedTransclude.transclude(selectedScope, function(caseElement) {
9498
selectedElements.push(caseElement);
95-
element.append(caseElement);
99+
selectedTransclude.element.after(caseElement);
96100
});
97101
});
98102
}
@@ -107,7 +111,7 @@ var ngSwitchWhenDirective = ngDirective({
107111
compile: function(element, attrs, transclude) {
108112
return function(scope, element, attr, ctrl) {
109113
ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
110-
ctrl.cases['!' + attrs.ngSwitchWhen].push(transclude);
114+
ctrl.cases['!' + attrs.ngSwitchWhen].push({ transclude: transclude, element: element });
111115
};
112116
}
113117
});
@@ -119,7 +123,7 @@ var ngSwitchDefaultDirective = ngDirective({
119123
compile: function(element, attrs, transclude) {
120124
return function(scope, element, attr, ctrl) {
121125
ctrl.cases['?'] = (ctrl.cases['?'] || []);
122-
ctrl.cases['?'].push(transclude);
126+
ctrl.cases['?'].push({ transclude: transclude, element: element });
123127
};
124128
}
125129
});

test/ng/directive/ngSwitchSpec.js

+55
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,61 @@ describe('ngSwitch', function() {
9595
}));
9696

9797

98+
it('should display the elements that do not match a switch', inject(function($rootScope, $compile) {
99+
element = $compile(
100+
'<ul ng-switch="select">' +
101+
'<li>always </li>' +
102+
'<li ng-switch-when="1">one </li>' +
103+
'<li ng-switch-when="2">two </li>' +
104+
'<li ng-switch-default>other, </li>' +
105+
'<li ng-switch-default>other too </li>' +
106+
'</ul>')($rootScope);
107+
$rootScope.$apply();
108+
expect(element.text()).toEqual('always other, other too ');
109+
$rootScope.select = 1;
110+
$rootScope.$apply();
111+
expect(element.text()).toEqual('always one ');
112+
}));
113+
114+
115+
it('should display the elements that do not have ngSwitchWhen nor ngSwitchDefault at the position specified in the template, when the first and last elements in the ngSwitch body do not have a ngSwitch* directive', inject(function($rootScope, $compile) {
116+
element = $compile(
117+
'<ul ng-switch="select">' +
118+
'<li>1</li>' +
119+
'<li ng-switch-when="1">2</li>' +
120+
'<li>3</li>' +
121+
'<li ng-switch-when="2">4</li>' +
122+
'<li ng-switch-default>5</li>' +
123+
'<li>6</li>' +
124+
'<li ng-switch-default>7</li>' +
125+
'<li>8</li>' +
126+
'</ul>')($rootScope);
127+
$rootScope.$apply();
128+
expect(element.text()).toEqual('135678');
129+
$rootScope.select = 1;
130+
$rootScope.$apply();
131+
expect(element.text()).toEqual('12368');
132+
}));
133+
134+
135+
it('should display the elements that do not have ngSwitchWhen nor ngSwitchDefault at the position specified in the template when the first and last elements in the ngSwitch have a ngSwitch* directive', inject(function($rootScope, $compile) {
136+
element = $compile(
137+
'<ul ng-switch="select">' +
138+
'<li ng-switch-when="1">2</li>' +
139+
'<li>3</li>' +
140+
'<li ng-switch-when="2">4</li>' +
141+
'<li ng-switch-default>5</li>' +
142+
'<li>6</li>' +
143+
'<li ng-switch-default>7</li>' +
144+
'</ul>')($rootScope);
145+
$rootScope.$apply();
146+
expect(element.text()).toEqual('3567');
147+
$rootScope.select = 1;
148+
$rootScope.$apply();
149+
expect(element.text()).toEqual('236');
150+
}));
151+
152+
98153
it('should call change on switch', inject(function($rootScope, $compile) {
99154
element = $compile(
100155
'<ng:switch on="url" change="name=\'works\'">' +

0 commit comments

Comments
 (0)