Prevent initial select option being blank in Angular

Darwin Tech

I have some simple angular code which renders a form and populates a select box with options from an array:

html:

<form class="form-horizontal">
  <div class="control-group">
      Title: <br />
      <input ng-model="new_project.title" type="text" id="title" class="form-control" placeholder="Title" >
      Created By: <br />
      <select data-ng-options="o.name for o in assignedToOptions" data-ng-model="new_project.created_by" id="created_by" class="form-control"></select>
  </div>
  ...
</form>

js:

$scope.getUsers = function () {
    ConcernService.list('users/').then(function(data) {
        $scope.users = data;
        // assign user list to select options
        $scope.assignedToOptions = [];
        i = 0;
        while (i < data.length) {
            $scope.assignedToOptions.push({ name: data[i].id, name: data[i].username });
            i++;
        };
    });
};

This all works fine, but Angular always places a blank option in the select field. What is the best way to eliminate the blank option?

tasseKATT

To eliminate the blank option you need to make sure that the selects model has an initial value.

You can set it in the controller:

$scope.new_project = 
    {
      created_by: $scope.assignedToOptions[0]
    };

Or by adding the following attribute to the select element:

ng-init="new_project.created_by = assignedToOptions[0]"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular ng-options select not being set to initial value

From Java

default select option as blank

From Dev

Angular select option shows blank when assigned from array

From Dev

Angular blank option selected

From Dev

ng option initial value select

From Dev

ng option initial value select

From Dev

Angular ng-options remove blank option and select the first option only

From Dev

Prevent blank datarow being inserted to datagridview

From Dev

Android: Prevent EditText in Alert from being blank

From Dev

MVC dropdown select (blank) option

From Dev

Blank select option in Form Output

From Dev

Remove Blank option from Select Option with AngularJS

From Dev

Select option not being displayed properly

From Dev

Angular ng-options remove blank or set default option in special select settings

From Dev

Remove Blank option tag from select list

From Dev

LocalStorage: Disabled Select Option Rendered Blank

From Dev

default select option as blank mvc razor

From Dev

How to disable blank option in select AngularJS?

From Dev

Blank page being shown- Angular JS

From Dev

Change between> or <being by selecting select option

From Dev

cannot remove blank option from angular

From Dev

cannot remove blank option from angular

From Dev

how to prevent onclick event to trigger in select option

From Dev

prevent angular template from being evaluated by browser

From Dev

prevent angular template from being evaluated by browser

From Dev

Select the blank option of select box programmatically in AngularJS directive

From Dev

Select option become blank when Select2 is applied

From Dev

How to prevent a closing </option> tag from being removed?

From Dev

Angular 2 Select Component set initial selection

Related Related

  1. 1

    Angular ng-options select not being set to initial value

  2. 2

    default select option as blank

  3. 3

    Angular select option shows blank when assigned from array

  4. 4

    Angular blank option selected

  5. 5

    ng option initial value select

  6. 6

    ng option initial value select

  7. 7

    Angular ng-options remove blank option and select the first option only

  8. 8

    Prevent blank datarow being inserted to datagridview

  9. 9

    Android: Prevent EditText in Alert from being blank

  10. 10

    MVC dropdown select (blank) option

  11. 11

    Blank select option in Form Output

  12. 12

    Remove Blank option from Select Option with AngularJS

  13. 13

    Select option not being displayed properly

  14. 14

    Angular ng-options remove blank or set default option in special select settings

  15. 15

    Remove Blank option tag from select list

  16. 16

    LocalStorage: Disabled Select Option Rendered Blank

  17. 17

    default select option as blank mvc razor

  18. 18

    How to disable blank option in select AngularJS?

  19. 19

    Blank page being shown- Angular JS

  20. 20

    Change between> or <being by selecting select option

  21. 21

    cannot remove blank option from angular

  22. 22

    cannot remove blank option from angular

  23. 23

    how to prevent onclick event to trigger in select option

  24. 24

    prevent angular template from being evaluated by browser

  25. 25

    prevent angular template from being evaluated by browser

  26. 26

    Select the blank option of select box programmatically in AngularJS directive

  27. 27

    Select option become blank when Select2 is applied

  28. 28

    How to prevent a closing </option> tag from being removed?

  29. 29

    Angular 2 Select Component set initial selection

HotTag

Archive