Sikandar Sahab
Sikandar Sahab

Reputation: 708

How to make pre-selection in ng-options

I have a Business dropdown, that is being populated with a list of objects. All I want to do is when I load the page I have business pre-selected with the data coming from my service.

Let's go through details

json array of objects for the select option

[{"code":"MA","value":"Main"},{"code":"TL","value":"Toll Free"},{"code":"AP","value":"After Hour"},{"code":"FX","value":"Fax"},{"code":"DR","value":"Direct"},{"code":"WB","value":"Web"},{"code":"EM","value":"Email"}]

code for select option

<select id="selectOption" ng-model="company.business" ng-options="option as 
option.value for option in businessDropdownValues" class="form-control form- 
control-sm input-sm"> </select>

Please tell me how do I get the pre-selected option if the company.business is received as one of the objects received from the list provided above?

Thanks

Upvotes: 1

Views: 34

Answers (2)

shivlal kumavat
shivlal kumavat

Reputation: 888

You have to use like this.

 <select id="selectOption" ng-model="company.business" ng-options="option.value for option in businessDropdownValues track by option.code" class="form-control form- 
control-sm input-sm"> </select>

Upvotes: 0

shivlal kumavat
shivlal kumavat

Reputation: 888

You can also do like this :

<select id="selectOption" ng-model="company.business" ng-options=" 
option.value for option in businessDropdownValues track by option.id" class="form-control form- 
control-sm input-sm"> </select>

Upvotes: 1

Related Questions