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.
Try this one in your controller, in the same order:
If you use ng-init your model to solve this problem:
Hi I had some jQUery mobile references and I removed them. With jQuery mobile any of above fixes did not work for me. ( Its great if someone can explain the conflict between jQuery Mobile and Angular JS )
https://guntucomputerhacks.blogspot.com.au/2017/10/angular-js-drop-down-first-options-vs.html
Though both @pkozlowski.opensource's and @Mark's answers are correct, I'd like to share my slightly modified version where I always select the first item in the list, regardless of its value:
I faced the same issue. If you are posting an angular form with normal post then you will face this issue, as angular don't allow you to set values for the options in the way you have used. If you get the value of "form.type" then you will find the right value. You have to post the angular object it self not the form post.
Here is the fix :
for a sample data like :
The select option should be like this:-
The point being when we write
value.listCount
asvalue.listName
, it automatically populates the text invalue.listName
but the value of the selected option isvalue.listCount
although the values my show normal 0,1,2 .. and so on !!!In my case, the
financeRef.financeLimit
is actually grabbing thevalue.listCount
and I can do my manipulation in the controller dynamically.