Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How can I add empty option in the beginning when I use ng-option to provide predefined list of options which doesn't include empty one?

An example in jade

select(ng-model="property.Value", ng-options="item.Value as item.Name for item in property.Choices")
share|improve this question
up vote 46 down vote accepted

Just add an option with empty value inside of the select.

select(ng-model="property.Value", ng-options="item.Value as item.Name for item in property.Choices")
  option(value="")

Here's an example - http://jsfiddle.net/sseletskyy/uky9m/1/

share|improve this answer
1  
Thank you very much! – Nathan Bertram Apr 7 '14 at 20:09
1  
What if the data comes from a services for example? Besides that I generally try to avoid view-specific things in the controller. It would be great if there was a way to do that in the view. Maybe using a directive?! – fnx Jul 15 '14 at 15:20
    
not working if you choose an option and then like to change back to empty option. – dang May 10 '16 at 18:52
    
@dang please provide an example when it doesn't work e.g. here is my another example - codepen.io/sseletskyy/pen/KzYaQq – Serge Seletskyy May 13 '16 at 13:20

just modify the property.choices array in angular.

$scope.property.Choices.splice(0, 0, {"id":"0","Name":"Richard"});

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.