Sunday, 15 September 2013

Why does angularjs include an empty option in select -



Why does angularjs include an empty option in select -

i've been working angular lastly few weeks , 1 thing bothering me after trying permutations or configuration defined in spec @ http://docs.angularjs.org/api/ng.directive:select, still empty alternative first kid of select element.

here's jade

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

here controller

$scope.typeoptions = [ { name: 'feature', value: 'feature' }, { name: 'bug', value: 'bug' }, { name: 'enhancement', value: 'enhancement' } ];

finally, here's html gets generated

<select ng-model="form.type" required="required" ng-options="option.value option.name alternative 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 need rid of it?

p.s. things work without well, looks odd if utilize select2 without multiple selection.

the empty option generated when value referenced ng-model doesn't exist in set of options passed ng-options. happens prevent accidental model selection: angularjs can see initial model either undefined or not in set of options , don't want decide model value on own.

if want rid of empty alternative select initial value in controller, like:

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

here jsfiddle: http://jsfiddle.net/mtfrd/3/

in short: empty alternative means no valid model selected (by valid mean: set of options). need select valid model value rid of empty option.

angularjs

No comments:

Post a Comment