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

Commit e394ec3

Browse files
lgalfasoIgorMinar
authored andcommitted
feat(ngSwitch): support multiple matches on ngSwitchWhen and ngSwitchDefault
Closes #1074
1 parent e19b04c commit e394ec3

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

Diff for: src/ng/directive/ngSwitch.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
* On child elments add:
2121
*
2222
* * `ngSwitchWhen`: the case statement to match against. If match then this
23-
* case will be displayed.
24-
* * `ngSwitchDefault`: the default case when no other casses match.
23+
* case will be displayed. If the same match appears multiple times, all the
24+
* elements will be displayed.
25+
* * `ngSwitchDefault`: the default case when no other case match. If there
26+
* are multiple default cases, all of them will be displayed when no other
27+
* case match.
2528
*
2629
* @example
2730
<doc:example>
@@ -69,22 +72,28 @@ var ngSwitchDirective = valueFn({
6972
}],
7073
link: function(scope, element, attr, ctrl) {
7174
var watchExpr = attr.ngSwitch || attr.on,
72-
selectedTransclude,
73-
selectedElement,
74-
selectedScope;
75+
selectedTranscludes,
76+
selectedElements,
77+
selectedScopes;
7578

7679
scope.$watch(watchExpr, function ngSwitchWatchAction(value) {
77-
if (selectedElement) {
80+
forEach(selectedScopes, function(selectedScope) {
7881
selectedScope.$destroy();
82+
});
83+
forEach(selectedElements, function(selectedElement) {
7984
selectedElement.remove();
80-
selectedElement = selectedScope = null;
81-
}
82-
if ((selectedTransclude = ctrl.cases['!' + value] || ctrl.cases['?'])) {
85+
});
86+
selectedElements = [];
87+
selectedScopes = [];
88+
if ((selectedTranscludes = ctrl.cases['!' + value] || ctrl.cases['?'])) {
8389
scope.$eval(attr.change);
84-
selectedScope = scope.$new();
85-
selectedTransclude(selectedScope, function(caseElement) {
86-
selectedElement = caseElement;
87-
element.append(caseElement);
90+
forEach(selectedTranscludes, function(selectedTransclude) {
91+
var selectedScope = scope.$new();
92+
selectedScopes.push(selectedScope);
93+
selectedTransclude(selectedScope, function(caseElement) {
94+
selectedElements.push(caseElement);
95+
element.append(caseElement);
96+
});
8897
});
8998
}
9099
});
@@ -97,7 +106,8 @@ var ngSwitchWhenDirective = ngDirective({
97106
require: '^ngSwitch',
98107
compile: function(element, attrs, transclude) {
99108
return function(scope, element, attr, ctrl) {
100-
ctrl.cases['!' + attrs.ngSwitchWhen] = transclude;
109+
ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
110+
ctrl.cases['!' + attrs.ngSwitchWhen].push(transclude);
101111
};
102112
}
103113
});
@@ -108,7 +118,8 @@ var ngSwitchDefaultDirective = ngDirective({
108118
require: '^ngSwitch',
109119
compile: function(element, attrs, transclude) {
110120
return function(scope, element, attr, ctrl) {
111-
ctrl.cases['?'] = transclude;
121+
ctrl.cases['?'] = (ctrl.cases['?'] || []);
122+
ctrl.cases['?'].push(transclude);
112123
};
113124
}
114125
});

Diff for: test/ng/directive/ngSwitchSpec.js

+40
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,32 @@ describe('ngSwitch', function() {
3535
expect(element.text()).toEqual('true:misko');
3636
}));
3737

38+
it('should switch show all the options that match the switch-when', inject(function($rootScope, $compile) {
39+
element = $compile(
40+
'<ul ng-switch="select">' +
41+
'<li ng-switch-when="1">first:{{name}}</li>' +
42+
'<li ng-switch-when="1">, first too:{{name}}</li>' +
43+
'<li ng-switch-when="2">second:{{name}}</li>' +
44+
'<li ng-switch-when="true">true:{{name}}</li>' +
45+
'</ul>')($rootScope);
46+
expect(element.html()).toEqual(
47+
'<!-- ngSwitchWhen: 1 --><!-- ngSwitchWhen: 1 --><!-- ngSwitchWhen: 2 --><!-- ngSwitchWhen: true -->');
48+
$rootScope.select = 1;
49+
$rootScope.$apply();
50+
expect(element.text()).toEqual('first:, first too:');
51+
$rootScope.name="shyam";
52+
$rootScope.$apply();
53+
expect(element.text()).toEqual('first:shyam, first too:shyam');
54+
$rootScope.select = 2;
55+
$rootScope.$apply();
56+
expect(element.text()).toEqual('second:shyam');
57+
$rootScope.name = 'misko';
58+
$rootScope.$apply();
59+
expect(element.text()).toEqual('second:misko');
60+
$rootScope.select = true;
61+
$rootScope.$apply();
62+
expect(element.text()).toEqual('true:misko');
63+
}));
3864

3965
it('should switch on switch-when-default', inject(function($rootScope, $compile) {
4066
element = $compile(
@@ -49,6 +75,20 @@ describe('ngSwitch', function() {
4975
expect(element.text()).toEqual('one');
5076
}));
5177

78+
it('should show all switch-when-default', inject(function($rootScope, $compile) {
79+
element = $compile(
80+
'<ul ng-switch="select">' +
81+
'<li ng-switch-when="1">one</li>' +
82+
'<li ng-switch-default>other</li>' +
83+
'<li ng-switch-default>, other too</li>' +
84+
'</ul>')($rootScope);
85+
$rootScope.$apply();
86+
expect(element.text()).toEqual('other, other too');
87+
$rootScope.select = 1;
88+
$rootScope.$apply();
89+
expect(element.text()).toEqual('one');
90+
}));
91+
5292

5393
it('should call change on switch', inject(function($rootScope, $compile) {
5494
element = $compile(

0 commit comments

Comments
 (0)