add default option to angularjs select

dagda1

I am trying to add a default option to a select with angular but it adds a blank option which strangely disappears when I select it.

I am getting the object to bind to the select using ng-options like this. I am appending a default option like this:

var DocumentSearchController = function (documentsService) {
    documentsService.getDocTypes().then(function (results) {
        this.documentTypes = results.data;
        this.documentTypes.unshift({ DocumentTypeID: null, Name: 'Select a Document Type' });
    }.bind(this));

The structure of the objects is:

[{DocumentTypeID: 1, Name: 'Blah'}]

I then use ng-options to bind the objects:

<select class="form-control"
        id="documentTypeSelector"
        name="documentTypeSelector"
        ng-model="vm.selectedDocumentType"
        ng-options="option.Name for option in vm.documentTypes | orderBy: 'option.DocumentTypeID':false track by option.DocumentTypeID">
</select>

Then annoying this is that a blank option is strangely added that is removed when I select an item.

The options are also not ordered so I suspect my list comprehension is wrong.

Rogerio Soares

You can add an default value this way :

<select class="form-control"
        id="documentTypeSelector"
        name="documentTypeSelector"
        ng-model="vm.selectedDocumentType"
        ng-options="option.Name for option in vm.documentTypes | orderBy: 'option.DocumentTypeID':false track by option.DocumentTypeID">
    <option value="">Select a Document Type</option>
</select>

Edit Or using init function

<select class="form-control"
        ng-init="vm.selectedDocumentType= vm.documentTypes[0]" 
        id="documentTypeSelector"
        name="documentTypeSelector"
        ng-model="vm.selectedDocumentType"
        ng-options="option.Name for option in vm.documentTypes | orderBy:'option.DocumentTypeID':false track by option.DocumentTypeID">
</select>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJs select add option

From Dev

AngularJS : how to create select option and set default select option?

From Dev

Default option in select is not working using AngularJS

From Dev

AngularJS - how to set default select option

From Dev

The default value is not ? object:null ? in select option Angularjs

From Dev

Add class to "select" if default option is selected

From Dev

How to add empty option in select in AngularJS?

From Dev

How to add disable option or default option in select box?

From Dev

AngularJS - ng-repeat default value for select option

From Dev

How do I set the default option for select with AngularJS?

From Dev

How to add a default "Select" option to this ASP.NET DropDownList control?

From Dev

How to add a default "Select" option to this ASP.NET DropDownList control?

From Java

default select option as blank

From Dev

HTML <select> default option

From Dev

default setting for option select

From Dev

AngularJS: Add Focus behavior to list items similar to `select-option`

From Dev

AngularJS select creates option with value "? object:null ?" and doesn't use empty default option when bound to null

From Dev

AngularJS: select option

From Dev

AngularJS: Set default select option when using directive to apply select drop down to page

From Dev

select first option in select angularjs

From Dev

Default Option not selected dropdown in angularjs

From Dev

Laravel - Select - set Default option

From Dev

Laravel Option Select - Default Issue

From Dev

Laravel Option Select - Default Issue

From Dev

change color of default option in select

From Dev

Jquery select option default option text

From Dev

ng-Select Option set default value from list if value match using angularJS

From Dev

ng-Select Option set default value from list if value match using angularJS

From Dev

Remove Blank option from Select Option with AngularJS

Related Related

  1. 1

    AngularJs select add option

  2. 2

    AngularJS : how to create select option and set default select option?

  3. 3

    Default option in select is not working using AngularJS

  4. 4

    AngularJS - how to set default select option

  5. 5

    The default value is not ? object:null ? in select option Angularjs

  6. 6

    Add class to "select" if default option is selected

  7. 7

    How to add empty option in select in AngularJS?

  8. 8

    How to add disable option or default option in select box?

  9. 9

    AngularJS - ng-repeat default value for select option

  10. 10

    How do I set the default option for select with AngularJS?

  11. 11

    How to add a default "Select" option to this ASP.NET DropDownList control?

  12. 12

    How to add a default "Select" option to this ASP.NET DropDownList control?

  13. 13

    default select option as blank

  14. 14

    HTML <select> default option

  15. 15

    default setting for option select

  16. 16

    AngularJS: Add Focus behavior to list items similar to `select-option`

  17. 17

    AngularJS select creates option with value "? object:null ?" and doesn't use empty default option when bound to null

  18. 18

    AngularJS: select option

  19. 19

    AngularJS: Set default select option when using directive to apply select drop down to page

  20. 20

    select first option in select angularjs

  21. 21

    Default Option not selected dropdown in angularjs

  22. 22

    Laravel - Select - set Default option

  23. 23

    Laravel Option Select - Default Issue

  24. 24

    Laravel Option Select - Default Issue

  25. 25

    change color of default option in select

  26. 26

    Jquery select option default option text

  27. 27

    ng-Select Option set default value from list if value match using angularJS

  28. 28

    ng-Select Option set default value from list if value match using angularJS

  29. 29

    Remove Blank option from Select Option with AngularJS

HotTag

Archive