Angularjs: How to include empty option in select when using ng-option
Asked Answered
P

3

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")
Paschall answered 27/3, 2013 at 1:11 Comment(0)
P
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/

Paschall answered 27/3, 2013 at 1:11 Comment(4)
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?!Abstruse
not working if you choose an option and then like to change back to empty option.Amateurish
@Amateurish please provide an example when it doesn't work e.g. here is my another example - codepen.io/sseletskyy/pen/KzYaQqPaschall
@SergeSeletskyy dang is referring to the fiddle you provided, the first example with the "Show empty option by default" cannot be changed back to blankBuffum
O
1

just modify the property.choices array in angular.

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

Oder answered 29/4, 2014 at 17:29 Comment(0)
P
1

This just worked for me by modifying the object

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

Piane answered 21/8, 2017 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.