Skip to content

Commit 33732ce

Browse files
fix(ngOptions): allow falsy values as option group identifiers
Now one can use `''`, `0`, `false` and `null` as option groups. Previously all of these falsy values were treated as the option not being a member of a group. Closes angular#7015 Closes angular#7024 BREAKING CHANGES If your data contains falsy values for option groups, then these options will now be placed into option groups. Only option groups that are `undefined` will result in the option being put in no group. If you have data that contains falsy values that should not be used as groups then you must filter the values before passing them to `ngOptions` converting falsy values to `undefined`.
1 parent 658a865 commit 33732ce

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/ng/directive/ngOptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
653653
var groupElement;
654654
var optionElement;
655655

656-
if (option.group) {
656+
if (isDefined(option.group)) {
657657

658658
// This option is to live in a group
659659
// See if we have already created this group

test/ng/directive/ngOptionsSpec.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -1497,28 +1497,37 @@ describe('ngOptions', function() {
14971497

14981498
scope.$apply(function() {
14991499
scope.values = [{name: 'A'},
1500-
{name: 'B', group: 'first'},
1501-
{name: 'C', group: 'second'},
1502-
{name: 'D', group: 'first'},
1503-
{name: 'E', group: 'second'}];
1500+
{name: 'B', group: 0},
1501+
{name: 'C', group: 'first'},
1502+
{name: 'D', group: 'second'},
1503+
{name: 'E', group: 0},
1504+
{name: 'F', group: 'first'},
1505+
{name: 'G', group: 'second'}];
15041506
scope.selected = scope.values[3];
15051507
});
15061508

15071509
expect(element).toEqualSelectValue(scope.selected);
15081510

1509-
var first = jqLite(element.find('optgroup')[0]);
1510-
var b = jqLite(first.find('option')[0]);
1511-
var d = jqLite(first.find('option')[1]);
1512-
expect(first.attr('label')).toEqual('first');
1511+
var zero = jqLite(element.find('optgroup')[0]);
1512+
var b = jqLite(zero.find('option')[0]);
1513+
var e = jqLite(zero.find('option')[1]);
1514+
expect(zero.attr('label')).toEqual('0');
15131515
expect(b.text()).toEqual('B');
1514-
expect(d.text()).toEqual('D');
1516+
expect(e.text()).toEqual('E');
15151517

1516-
var second = jqLite(element.find('optgroup')[1]);
1517-
var c = jqLite(second.find('option')[0]);
1518-
var e = jqLite(second.find('option')[1]);
1519-
expect(second.attr('label')).toEqual('second');
1518+
var first = jqLite(element.find('optgroup')[1]);
1519+
var c = jqLite(first.find('option')[0]);
1520+
var f = jqLite(first.find('option')[1]);
1521+
expect(first.attr('label')).toEqual('first');
15201522
expect(c.text()).toEqual('C');
1521-
expect(e.text()).toEqual('E');
1523+
expect(f.text()).toEqual('F');
1524+
1525+
var second = jqLite(element.find('optgroup')[2]);
1526+
var d = jqLite(second.find('option')[0]);
1527+
var g = jqLite(second.find('option')[1]);
1528+
expect(second.attr('label')).toEqual('second');
1529+
expect(d.text()).toEqual('D');
1530+
expect(g.text()).toEqual('G');
15221531

15231532
scope.$apply(function() {
15241533
scope.selected = scope.values[0];

0 commit comments

Comments
 (0)