2017-04-06 79 views
1

我有以下select list如何在選擇列表中將ID設置爲值?

<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 from object?

+0

'納克選項= 「我在設備i.id對於i」'和'NG-模型= 「i.id」'和'NG-改變=「wheteverMethod()」'你會在控制器中得到'ng-model'值whateverMethod() –

回答

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>

+0

Bu如何在ng-change中傳遞i.id? – Daniel

+0

查看更新的答案。您可以將模型值傳遞給'ng-change'函數 –

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

+0

如何在ng-change中傳遞i.id? – Daniel

+0

'$ scope.selectVersion = function(){ console.log($ scope.selected_version_os_to); }' –

相關問題