如何在ng-options中进行预选

时间:2018-04-02 08:26:16

标签: arrays angularjs json

我有一个Business下拉列表,其中包含一个对象列表。我想要做的就是当我加载页面时,我预先选择了来自我服务的数据。

让我们详细了解

选择选项的

json对象数组

[{"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"}]

选择选项的代码

<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>

如果收到company.business作为从上面提供的列表中收到的对象之一,请告诉我如何获得预先选择的选项?

由于

2 个答案:

答案 0 :(得分:2)

你也可以这样做:

<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>

答案 1 :(得分:1)

你必须这样使用。

 <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>