Skip to content

Commit 6ca3abb

Browse files
committed
fix(ngOption): accept 0 as an option group
Accept `0` and an option group Close angular#7015 BREAKING CHANGE When using option groups at an ng-option, the literal `0` will be handled as a group with label `'0'`. Use the literal string `''` or undefined for options that should not be in any option group
1 parent 64d4046 commit 6ca3abb

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/ng/directive/select.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,11 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
315315
var displayFn = $parse(match[2] || match[1]),
316316
valueName = match[4] || match[6],
317317
keyName = match[5],
318-
groupByFn = $parse(match[3] || ''),
318+
groupByGet = $parse(match[3] || ''),
319+
groupByFn = function (scope, locals) {
320+
var result = groupByGet(scope, locals);
321+
return isUndefined(result) ? '' : result;
322+
},
319323
valueFn = $parse(match[2] ? match[1] : valueName),
320324
valuesFn = $parse(match[7]),
321325
track = match[8],
@@ -455,7 +459,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
455459

456460
locals[valueName] = values[key];
457461

458-
optionGroupName = groupByFn(scope, locals) || '';
462+
optionGroupName = groupByFn(scope, locals);
459463
if (!(optionGroup = optionGroups[optionGroupName])) {
460464
optionGroup = optionGroups[optionGroupName] = [];
461465
optionGroupNames.push(optionGroupName);

test/ng/directive/selectSpec.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -782,28 +782,37 @@ describe('select', function() {
782782

783783
scope.$apply(function() {
784784
scope.values = [{name: 'A'},
785-
{name: 'B', group: 'first'},
786-
{name: 'C', group: 'second'},
787-
{name: 'D', group: 'first'},
788-
{name: 'E', group: 'second'}];
785+
{name: 'B', group: 0},
786+
{name: 'C', group: 'first'},
787+
{name: 'D', group: 'second'},
788+
{name: 'E', group: 0},
789+
{name: 'F', group: 'first'},
790+
{name: 'G', group: 'second'}];
789791
scope.selected = scope.values[3];
790792
});
791793

792794
expect(element.val()).toEqual('3');
793795

794-
var first = jqLite(element.find('optgroup')[0]);
795-
var b = jqLite(first.find('option')[0]);
796-
var d = jqLite(first.find('option')[1]);
797-
expect(first.attr('label')).toEqual('first');
796+
var zero = jqLite(element.find('optgroup')[0]);
797+
var b = jqLite(zero.find('option')[0]);
798+
var e = jqLite(zero.find('option')[1]);
799+
expect(zero.attr('label')).toEqual('0');
798800
expect(b.text()).toEqual('B');
799-
expect(d.text()).toEqual('D');
801+
expect(e.text()).toEqual('E');
800802

801-
var second = jqLite(element.find('optgroup')[1]);
802-
var c = jqLite(second.find('option')[0]);
803-
var e = jqLite(second.find('option')[1]);
804-
expect(second.attr('label')).toEqual('second');
803+
var first = jqLite(element.find('optgroup')[1]);
804+
var c = jqLite(first.find('option')[0]);
805+
var f = jqLite(first.find('option')[1]);
806+
expect(first.attr('label')).toEqual('first');
805807
expect(c.text()).toEqual('C');
806-
expect(e.text()).toEqual('E');
808+
expect(f.text()).toEqual('F');
809+
810+
var second = jqLite(element.find('optgroup')[2]);
811+
var d = jqLite(second.find('option')[0]);
812+
var g = jqLite(second.find('option')[1]);
813+
expect(second.attr('label')).toEqual('second');
814+
expect(d.text()).toEqual('D');
815+
expect(g.text()).toEqual('G');
807816

808817
scope.$apply(function() {
809818
scope.selected = scope.values[0];

0 commit comments

Comments
 (0)