Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default value in select drop-down using Angularjs

Tags:

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"

like image 462
runtimeZero Avatar asked Jul 23 '13 15:07

runtimeZero


People also ask

How to set default selected value in dropdown AngularJS?

$scope. payment = { type : 'Cash' } this will give you Cash as a default selected value. If i set on my controller, bydefault value is loading, but my select option change is not working. that is on change on option ng-if content is not loading.

How to Select default value for drop down in Angular?

Use ngValue and option element to set default value in dropdown list. You can set default value in dropdown list using ngValue directive and by hard-coding option element as shown in the example below. Let us set default text like “Select User” and disable it from user selecting that option.

How do I change the default value in drop down?

Use the <select> element for this, which is a select box, also called drop down box, with option to list down items. Also, you can set the default value from the dropdown list of items in HTML forms. For that, add selected in the <option> tag for the value you want to preselect.

Why angular dropdown automatically adds an empty value?

Hi Nishu, I guess your problem is AngularJS Dropdown automatically adds an Empty Value and you want to remove that blank value. The empty option is generated when a value referenced by ng-model doesn't exist in a set of options passed to ng-options. This happens to prevent accidental model selection.


1 Answers

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 
like image 142
Stephanie Avatar answered Oct 14 '22 19:10

Stephanie