22

I have an object as below. I have to display this as a drop-down:

var list = [{id:4,name:"abc"},{id:600,name:"def"},{id:200,name:"xyz"}]

In my controller I have a variable that carries a value. This value decided which of the above three items in the array will be selected by default in the drop-down:

 $scope.object.setDefault = 600;

When I create a drop-down form item as below:

<select ng-model="object.setDefault" ng-options="r.name for r in list">                 

I face two problems:

  1. the list is generated as

    <option value="0">abc</option>
    <option value="1">def</option>
    <option value="2">xyz</option>
    

    instead of

    <option value="4">abc</option>
    <option value="600">def</option>
    <option value="200">xyz</option>
    
  2. No option gets selected by default even though i have ng-model="object.setDefault"

2
  • 1
    In angular the value attribute in options in index of option inside inside the select element Commented Jul 23, 2013 at 15:58
  • Ajay..you seem to be right about that. So now the question is, how do I give it a custom value Commented Jul 23, 2013 at 16:55

10 Answers 10

21

Problem 1:

The generated HTML you're getting is normal. Apparently it's a feature of Angular to be able to use any kind of object as value for a select. Angular does the mapping between the HTML option-value and the value in the ng-model. Also see Umur's comment in this question: How do I set the value property in AngularJS' ng-options?

Problem 2:

Make sure you're using the following ng-options:

<select ng-model="object.item" ng-options="item.id as item.name for item in list" />

And put this in your controller to select a default value:

object.item = 4
Sign up to request clarification or add additional context in comments.

Comments

10

When you use ng-options to populate a select list, it uses the entire object as the selected value, not just the single value you see in the select list. So in your case, you'd need to set

$scope.object.setDefault = {
    id:600,
    name:"def"
};

or

$scope.object.setDefault = $scope.selectItems[1];

I also recommend just outputting the value of $scope.object.setDefault in your template to see what I'm talking about getting selected.

<pre>{{object.setDefault}}</pre>

5 Comments

Only the second one would work. select uses === to compare values. If you set another object with the same attributes, it will be considered different.
Good to know. I always use the second method myself, but figured an identical object would work.
Another option is to use the select as label for value in array to bind the model to a property on value
Sharondio, Before I get to that point I need to understand how I can set custom values in "values" attribute of the <OPTION> tag ?
You can't. ng-options creates the index. If you use jonnyynnoj's suggestion, it will give you the id as the selected value, but the select will still show the array index as the value. If you absolutely need the option value to be your id, you could use ng-repeat on the option element and populate it anyway you want like this: plnkr.co/edit/3riXsi?p=preview
8

In View

<select ng-model="boxmodel"><option ng-repeat="lst in list" value="{{lst.id}}">{{lst.name}}</option></select>

JS:

In side controller

 $scope.boxModel = 600;

Comments

7

You can do it with following code(track by),

<select ng-model="modelName" ng-options="data.name for data in list track by data.id" ></select>

Comments

3

This is an old question and you might have got the answer already.

My plnkr explains on my approach to accomplish selecting a default dropdown value. Basically, I have a service which would return the dropdown values [hard coded to test]. I was not able to select the value by default and almost spend a day and finally figured out that I should have set $scope.proofGroupId = "47"; instead of $scope.proofGroupId = 47; in the script.js file. It was my bad and I did not notice that I was setting an integer 47 instead of the string "47". I retained the plnkr as it is just in case if some one would like to see. Hopefully, this would help some one.

Comments

3
<select ng-init="somethingHere = options[0]" ng-model="somethingHere" ng-options="option.name for option in options"></select>

This would get you desired result Dude :) Cheers

Comments

2

Some of the scenarios, object.item would not be loaded or will be undefined.

Use ng-init

<select ng-init="object.item=2" ng-model="object.item"
ng-options="item.id as item.name for item in list"

1 Comment

ng-options is enumerating my list 0..10 and so on irregardless of the actual id value. Nevermind. It's just rendering like that. The actual values are being passed in the form submit.
2
$scope.item = {
    "id": "3",
    "name": "ALL",
};

$scope.CategoryLst = [
    { id: '1', name: 'MD' },
    { id: '2', name: 'CRNA' },
    { id: '3', name: 'ALL' }];

<select ng-model="item.id" ng-selected="3" ng-options="i.id as i.name for i in CategoryLst"></select>

Comments

-3

we should use name value pair binding values into dropdown.see the code for more details

function myCtrl($scope) {
     $scope.statusTaskList = [
        { name: 'Open', value: '1' },
        { name: 'In Progress', value: '2' },
        { name: 'Complete', value: '3' },
        { name: 'Deleted', value: '4' },
    ];
    $scope.atcStatusTasks = $scope.statusTaskList[0]; // 0 -> Open 
}
<select ng-model="atcStatusTasks" ng-options="s.name for s in statusTaskList"></select>

Comments

-12

I could help you out with the html:

<option value="">abc</option>

instead of

<option value="4">abc</option>

to set abc as the default value.

Comments

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.