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")
dan1st
  • 12,568
  • 8
  • 34
  • 67
Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80

3 Answers3

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/

Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80
  • 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 - http://codepen.io/sseletskyy/pen/KzYaQq – Sergiy Seletskyy May 13 '16 at 13:20
  • @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 – Amicable Mar 16 '17 at 10:38
1

just modify the property.choices array in angular.

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

Richard Torcato
  • 2,504
  • 25
  • 26
1

This just worked for me by modifying the object

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

MrsPop88
  • 303
  • 4
  • 14