如何在选择列表中将id设置为值?

时间:2017-04-06 08:06:04

标签: angularjs

我有以下select list

<select ng-options="i.id as i.os_version for i in devices"
ng-model="selected_version_os_to" ng-change="selectVersion(i.id)">
</select>

对象devices看起来像这样: enter image description here

结果我得到了这个选择列表元素:

<option label="10.3" value="number:1">10.3</option>

如何将值选项设置为:id来自对象?

2 个答案:

答案 0 :(得分:2)

更新html将如下所示

HTML视图

    <select ng-options="i.os_version as i.os_version for i in devices" ng-model="selected_version_os_to" ng-change="selectVersion(i.id)" class="ng-valid ng-dirty ng-valid-parse ng-touched">
      <option label="windows" value="string:windows">windows</option>
      <option label="Mac" value="string:Mac">Mac</option>
      <option label="Lunix" value="string:Lunix">Lunix</option>
    </select>

jsfiddle link

答案 1 :(得分:1)

尝试使用此

ng-options="i.id as i.os_version for i in devices track by i.id"

<强> 演示

var app = angular.module('anApp', []);
app.controller('ctrl', function($scope) {
  $scope.devices = [{
    "id": 10,
    "os_version": "10.3"
  }, {
    "id": 20,
    "os_version": "11.3"
  }];
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>

<div ng-app="anApp" ng-controller="ctrl">
  <div class="form-group">
    <select ng-options="i.id as i.os_version for i in devices track by i.id" ng-model="selected_version_os_to" ng-change="selectVersion(selected_version_os_to)">
</select>
  </div>
</div>