Skip to content

Commit 41010ef

Browse files
committed
Dynamic option can hold empty value now so you don't have to initially put it inside select (issue angular#11470)
1 parent 9dfa949 commit 41010ef

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/ng/directive/select.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ var SelectController =
8383

8484

8585
// Tell the select control that an option, with the given value, has been added
86-
self.addOption = function(value) {
86+
self.addOption = function(value, element) {
8787
assertNotHasOwnProperty(value, '"option value"');
88+
if (value === '') {
89+
self.emptyOption = element
90+
}
8891
var count = optionsMap.get(value) || 0;
8992
optionsMap.put(value, count + 1);
9093
};

test/ng/directive/selectSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,42 @@ describe('select', function() {
200200

201201
describe('empty option', function() {
202202

203+
it ('should allow dynamic empty option', function() {
204+
scope.dynamicOptions = [];
205+
206+
compile('<select ng-model="dynamicEmpty">' +
207+
'<option ng-repeat="opt in dynamicOptions" value="{{opt.val}}">' +
208+
'{{opt.display}}' +
209+
'</option>' +
210+
'</selec>');
211+
212+
expect(element.find('option').length).toBe(1, 'Initially there will be one empty option');
213+
expect(element.find('option').val()).toBe('? undefined:undefined ?', 'Initially there will be one empty option with value ?');
214+
215+
// when dynamicOptions change and one of the elements is empty option self.emptyOption in directive should be defined
216+
scope.dynamicOptions = [
217+
{
218+
val : '',
219+
display : 'All Options'
220+
},
221+
{
222+
val : '1',
223+
display : 'First Option'
224+
}
225+
];
226+
scope.dynamicEmpty = '';
227+
228+
scope.$digest();
229+
230+
expect(element.find('option').length).toBe(2, 'There should be two option now');
231+
232+
expect(angular.element(element.find('option')[0]).val()).toBe('', 'First value should be empty');
233+
expect(angular.element(element.find('option')[0]).text()).toBe('All Options', 'First text should be "All Options"');
234+
expect(angular.element(element.find('option')[1]).val()).toBe('1', 'Second value should be empty');
235+
expect(angular.element(element.find('option')[1]).text()).toBe('First Option', 'Second text should be "First Option"');
236+
237+
});
238+
203239
it('should select the empty option when model is undefined', function() {
204240
compile('<select ng-model="robot">' +
205241
'<option value="">--select--</option>' +

0 commit comments

Comments
 (0)