23

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")

3 Answers 3

52

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/

Sign up to request clarification or add additional context in comments.

4 Comments

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?!
not working if you choose an option and then like to change back to empty option.
@dang please provide an example when it doesn't work e.g. here is my another example - codepen.io/sseletskyy/pen/KzYaQq
@SergeSeletskyy dang is referring to the fiddle you provided, the first example with the "Show empty option by default" cannot be changed back to blank
1

just modify the property.choices array in angular.

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

Comments

1

This just worked for me by modifying the object

angular.extend({'Please Select' : ''},objectName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.