Daniel
Daniel

Reputation: 1755

How to set id as value in select list?

I have the following 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>

And object devices looks like this: enter image description here

As a result I get this select list element:

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

How to set value options as: id from object?

Upvotes: 1

Views: 1332

Answers (2)

Hadi
Hadi

Reputation: 17299

try use this

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

Demo

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>

Upvotes: 1

Jayant Patil
Jayant Patil

Reputation: 1587

update html will look like below

HTML view

    <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

Upvotes: 2

Related Questions