Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(select): add auto-inference of option value
Browse files Browse the repository at this point in the history
This makes md-option follow HTML5 behavior of setting the value to
the text value of the option if no value is specified.

closes #1713
  • Loading branch information
rschmukler committed Apr 5, 2015
1 parent 13b9c69 commit 342af6a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ function OptionDirective($mdInkRipple, $mdUtil) {
} else if (angular.isDefined(attr.value)) {
setOptionValue(attr.value);
} else {
throw new Error("Expected either ngValue or value attr");
debugger;
scope.$watch(function() { return element.text(); }, setOptionValue)
}

attr.$observe('selected', function(selected) {
Expand Down
8 changes: 8 additions & 0 deletions src/components/select/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,20 @@ describe('<md-select-menu>', function() {
}));
});

it('auto-infers a value when none specified', inject(function($rootScope) {
$rootScope.name = "Hannah";
var el = setup('ng-model="name"', '<md-option>Tom</md-option>' +
'<md-option>Hannah</md-option>');
expect(selectedOptions(el).length).toBe(1);
}));

it('errors for duplicate md-options, non-dynamic value', inject(function($rootScope) {
expect(function() {
setup('ng-model="$root.model"', '<md-option value="a">Hello</md-option>' +
'<md-option value="a">Goodbye</md-option>');
}).toThrow();
}));

it('errors for duplicate md-options, ng-value', inject(function($rootScope) {
setup('ng-model="$root.model"', '<md-option ng-value="foo">Hello</md-option>' +
'<md-option ng-value="bar">Goodbye</md-option>');
Expand Down

0 comments on commit 342af6a

Please sign in to comment.