Skip to content

Commit 7df8959

Browse files
committed
feat(directive): ng:switch
Added the ability to handle multiple matches on ng:switch-when and ng:switch-default Closes angular#1074
1 parent 7cc4063 commit 7df8959

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

src/ng/directive/ngSwitch.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,28 @@ var ngSwitchDirective = valueFn({
6868
},
6969
link: function(scope, element, attr, ctrl) {
7070
var watchExpr = attr.ngSwitch || attr.on,
71-
selectedTransclude,
72-
selectedElement,
73-
selectedScope;
71+
selectedTranscludes,
72+
selectedElements,
73+
selectedScopes;
7474

7575
scope.$watch(watchExpr, function ngSwitchWatchAction(value) {
76-
if (selectedElement) {
76+
angular.forEach(selectedScopes, function(selectedScope) {
7777
selectedScope.$destroy();
78+
});
79+
angular.forEach(selectedElements, function(selectedElement) {
7880
selectedElement.remove();
79-
selectedElement = selectedScope = null;
80-
}
81-
if ((selectedTransclude = ctrl.cases['!' + value] || ctrl.cases['?'])) {
81+
});
82+
selectedElements = [];
83+
selectedScopes = [];
84+
if ((selectedTranscludes = ctrl.cases['!' + value] || ctrl.cases['?'])) {
8285
scope.$eval(attr.change);
83-
selectedScope = scope.$new();
84-
selectedTransclude(selectedScope, function(caseElement) {
85-
selectedElement = caseElement;
86-
element.append(caseElement);
86+
angular.forEach(selectedTranscludes, function(selectedTransclude) {
87+
var selectedScope = scope.$new();
88+
selectedScopes.push(selectedScope);
89+
selectedTransclude(selectedScope, function(caseElement) {
90+
selectedElements.push(caseElement);
91+
element.append(caseElement);
92+
});
8793
});
8894
}
8995
});
@@ -96,7 +102,8 @@ var ngSwitchWhenDirective = ngDirective({
96102
require: '^ngSwitch',
97103
compile: function(element, attrs, transclude) {
98104
return function(scope, element, attr, ctrl) {
99-
ctrl.cases['!' + attrs.ngSwitchWhen] = transclude;
105+
ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
106+
ctrl.cases['!' + attrs.ngSwitchWhen].push(transclude);
100107
};
101108
}
102109
});
@@ -107,7 +114,8 @@ var ngSwitchDefaultDirective = ngDirective({
107114
require: '^ngSwitch',
108115
compile: function(element, attrs, transclude) {
109116
return function(scope, element, attr, ctrl) {
110-
ctrl.cases['?'] = transclude;
117+
ctrl.cases['?'] = (ctrl.cases['?'] || []);
118+
ctrl.cases['?'].push(transclude);
111119
};
112120
}
113121
});

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+
'<div ng-switch="select">' +
41+
'<div ng-switch-when="1">first:{{name}}</div>' +
42+
'<div ng-switch-when="1">, first too:{{name}}</div>' +
43+
'<div ng-switch-when="2">second:{{name}}</div>' +
44+
'<div ng-switch-when="true">true:{{name}}</div>' +
45+
'</div>')($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+
'<ng:switch on="select">' +
81+
'<div ng:switch-when="1">one</div>' +
82+
'<div ng:switch-default>other</div>' +
83+
'<div ng:switch-default>, other too</div>' +
84+
'</ng:switch>')($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)