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

fix(select): normalize options without value attrs #6519

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {

// find "null" option
for(var i = 0, children = element.children(), ii = children.length; i < ii; i++) {
if(children[i].nodeName === 'OPTION' && isUndefined(children.eq(i).attr('value'))) {
children[i].value = '';
}

if (children[i].value === '') {
emptyOption = nullOption = children.eq(i);
break;
Expand Down
25 changes: 25 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ describe('select', function() {
expect(element).toEqualSelect([''], 'x', 'y');
});

it('should support option without a value attribute', function() {
compile('<select ng-model="robot">' +
'<option>--select--</option>' +
'<option value="x">robot x</option>' +
'<option value="y">robot y</option>' +
'</select>');
expect(element).toEqualSelect(["? undefined:undefined ?"], "--select--", 'x', 'y');
});

it('should support option without a value with other HTML attributes', function() {
compile('<select ng-model="robot">' +
'<option data-foo="bar">--select--</option>' +
'<option value="x">robot x</option>' +
'<option value="y">robot y</option>' +
'</select>');
expect(element).toEqualSelect(["? undefined:undefined ?"], "--select--", 'x', 'y');
});

it('should support option without a value attribute using `ng-options`', function() {
scope.someOpts = [{name: 'foo'}, {name: 'foo'}];
compile('<select ng-model="robot" ng-options="r.name for someOpts in someOpts">' +
'<option>--select--</option>' +
'</select>');
expect(element).toEqualSelect([""],"0","1");
});

it('should support defining an empty option anywhere in the option list', function() {
compile('<select ng-model="robot">' +
Expand Down