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

fix(ngOptions): add ngEmptyValue attribute to ngOptions #13471

Closed
wants to merge 3 commits into from
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
12 changes: 12 additions & 0 deletions src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var ngOptionsMinErr = minErr('ngOptions');
* be nested into the `<select>` element. This element will then represent the `null` or "not selected"
* option. See example below for demonstration.
*
* Additionally, a default value can be specified for empty options using the `ng-empty-value` attribute.
* See Usage section for more details.
*
* ## Complex Models (objects or collections)
*
* By default, `ngModel` watches the model by reference, not value. This is important to know when
Expand Down Expand Up @@ -156,6 +159,9 @@ var ngOptionsMinErr = minErr('ngOptions');
* `value` variable (e.g. `value.propertyName`). With this the selection is preserved
* even when the options are recreated (e.g. reloaded from the server).
*
* @param {expression=} ngEmptyValue When an option with empty value is selected, whether hardcoded or created
* with ngOptions, the associated ngModel will be assigned the result of this expression.
*
* @example
<example module="selectExample">
<file name="index.html">
Expand Down Expand Up @@ -487,8 +493,14 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
}
};

var emptyValueParseFn = 'ngEmptyValue' in attr && attr.ngEmptyValue.length > 0
&& $parse(attr.ngEmptyValue);

selectCtrl.readValue = function readNgOptionsValue() {

// Allow a default value to be substituted for any options with empty values
if (selectElement.val() === '' && emptyValueParseFn) return emptyValueParseFn(scope);

var selectedOption = options.selectValueMap[selectElement.val()];

if (selectedOption && !selectedOption.disabled) {
Expand Down
30 changes: 30 additions & 0 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,36 @@ describe('ngOptions', function() {
expect(scope.selected).toEqual([]);
});

it('should be possible to specify a value for empty options', function() {
createSelect({
'ng-model':'selected',
'ng-empty-value': 'someVar',
'ng-options':'c for c in choices'
}, true);

scope.$apply(function() {
scope.choices = ['A','B','C'];
scope.selected = "";
scope.someVar = "foo";
});
//current implementation only works if select is not pristine, so we click it back and forth
setSelectValue(element, 3);
setSelectValue(element, 0);

//make sure the empty value is being evaluated as an Angular expression
expect(scope.selected).toBe("foo");

//make sure a change to the value given in ng-empty-value also changes the model
scope.$apply(function() {
scope.someVar = "bar";
});

setSelectValue(element, 3);
setSelectValue(element, 0);

expect(scope.selected).toBe("bar");

});

it('should be possible to use ngIf in the blank option', function() {
var option;
Expand Down