1

I am populating my multiple select like this :

<select ng-model="myAddress" ng-options="address.addressLines for address in search.result.addresses" >
</select>

And there is 2 items currently. But when I load the page it seems to add an extra blank row as such

<option value="?" selected="selected" label=""></option>

But then when I click on my actual 2 item rows the above error row disappears. Any idea what is causing this ?

1 Answer 1

1

When the select is initially loaded into the view, the model (myAddress) is not set from the controller. Angular needs a way to represent this, so it adds an empty option to the select box. Once a selection is made, it is populated and gets rid of the empty option because it is not defined in the list that is bound to the select.

If you don't want a blank option to appear, you need to set a valid $scope.myAddress in the controller to be used as the default.

Depending on what you want to accomplish, there are really three options.

  1. You keep it as it works now where an empty option is available before a selection is made.
  2. Set an initial value in the controller so there will be no empty option available.
  3. Always have an empty option available, even if a value has been set to the select.

Example here:

HTML:

<div ng-app="myApp" ng-controller="MainController">
    Select with no value set in controller:<br />
    <select ng-model="select1" ng-options="item.name for item in items">
    </select> {{select1}}
    <br /><br />
        Select with value set in controller:<br />
    <select ng-model="select2" ng-options="item.name for item in items">
    </select> {{select2}}
    <br /><br />
        Select empty option always available (even after selection):<br />
    <select ng-model="select3" ng-options="item.name for item in items">
        <option value=""></option>
    </select> {{select3}}
</div>

Javascript:

angular.module("myApp", [])

.controller("MainController", function ($scope) {
    $scope.items = [
        {id: 1, name: "Item 1"},
        {id: 2, name: "Item 2"},
        {id: 3, name: "Item 3"},
        {id: 4, name: "Item 4"}
    ];

    //initial value for select2
    $scope.select2 = $scope.items[0];    
});
Sign up to request clarification or add additional context in comments.

3 Comments

Ah ok if I dont want anything to be selected from initial view ? Anyway I guess the norm then is to default to first item in the list in the controller ?
Forgot to say with option 3, you can also specify the text to be used for the empty option (e.g. "-- Select a Value --") by using said text in the option tag.
I'm still running into this despite setting a value. Initially I was using a property of an object, so I set an independent variable with an arbitrary value included in the valid options. Still getting the blank.

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.