Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does AngularJS include an empty option in select?

Tags:

angularjs

I've been working with AngularJS for the last few weeks, and the one thing which is really bothering me is that even after trying all permutations or the configuration defined in the specification at http://docs.angularjs.org/api/ng.directive:select, I still get an empty option as the first child of select element.

Here's the Jade:

select.span9(ng-model='form.type', required, ng-options='option.value as option.name for option in typeOptions'); 

Here the controller:

$scope.typeOptions = [     { name: 'Feature', value: 'feature' },     { name: 'Bug', value: 'bug' },     { name: 'Enhancement', value: 'enhancement' } ]; 

Finally, here's the HTML which gets generated:

<select ng-model="form.type" required="required" ng-options="option.value as option.name for option in typeOptions" class="span9 ng-pristine ng-invalid ng-invalid-required">     <option value="?" selected="selected"></option>     <option value="0">Feature</option>     <option value="1">Bug</option>     <option value="2">Enhancement</option> </select> 

What do I need to do to get rid of it?

P.S.: Things work without this as well, but it just looks odd if you use select2 without multiple selection.

like image 560
Sudhanshu Avatar asked Sep 29 '12 17:09

Sudhanshu


People also ask

Why angular dropdown automatically adds an empty value?

The empty option is generated when a value referenced by ng-model doesn't exist in a set of options passed to ng-options . This happens to prevent accidental model selection: AngularJS can see that the initial model is either undefined or not in the set of options and don't want to decide model value on its own.

What does it mean when the value of the selected menu option is an empty string?

</option> , value="" is turned into just value . The value, when set to the empty string is simply discarded.

What is Ng select in AngularJS?

AngularJS ng-selected Directive The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .

How do I set default selected value in ng-options?

In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest. Essentially when you define the $scope property your select will bind to assign it the default value from your data array.


1 Answers

The empty option is generated when a value referenced by ng-model doesn't exist in a set of options passed to ng-options. This happens to prevent accidental model selection: AngularJS can see that the initial model is either undefined or not in the set of options and don't want to decide model value on its own.

If you want to get rid of the empty option just select an initial value in your controller, something like:

$scope.form.type = $scope.typeOptions[0].value; 

Here is the jsFiddle: http://jsfiddle.net/MTfRD/3/

In short: the empty option means that no valid model is selected (by valid I mean: from the set of options). You need to select a valid model value to get rid of this empty option.

like image 88
pkozlowski.opensource Avatar answered Oct 15 '22 12:10

pkozlowski.opensource