4
votes

I am working with AngularJS and X-editable from past few weeks and one thing which is bothering me is that select option provides a empty option as first child.
Here's the HTML

<span editable-select="curuser.roles" e-name="roles" e-ng-options="role as role.name for role in roles">
{{ showRoles() }}
</span>  

Here is output HTML :

<select name="roles" ng-options="role as role.name for role in roles">
   <option value="?" selected="selected"></option>
   <option value="0">Admin</option>
   <option value="1">Company</option>
   <option value="2">Manager</option>
</select>  

What do I need to do to get rid of this empty option?
Here is working JSFiddle

3
Try having ng-model on your select and initialize it onload: $scope.model = roles[0]; - AlwaysALearner
Since I am using x-editable, I tried adding ng-model on span tag and initialise it but it did not work for me. - Abhishek
Can you show a sample in jsfiddle or plunker what you have tried and did not work? The way @CodeHater is suggesting should suffice effortlessly. - dmahapatro

3 Answers

0
votes

You have not picked an answer so I thought I would submit the fiddle that I got to work for others that have similar problems. I updated your editable-select and your e-ng-options. Here is the updated fiddle http://jsfiddle.net/7e7kk/22/

  editable-select="roles[0].value" 
  ng-options="role.value as role.name for role in roles"
0
votes

This is the normal behaviour. I have updated your jsfiddle to add a standard select element with ngOptions and as you can see there will be a blank value unless 1 of the options is selected: https://jsfiddle.net/vw4mkbou/2/

You need to either set the default value to 1 of the available options or have an empty state: http://jsfiddle.net/vw4mkbou/4/

0
votes

Change your code from:

e-ng-options = "role as role.name for role in roles"

to

e-ng-options="role.id as role.name for role in roles track by role.id"

And in the controller, add $scope.apply.

And of course, in your controller, set the default value of newly added rows to be roles[0].

Sorry, if this is too late. I've just seen your question.