我沒有得到所選標籤項的選項值的id值。id as ng-model for select in angularjs
<div ng-controller="UserCreationCtrl">
<form novalidate="novalidate" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputFirstName">First name:</label>
<div class="controls">
<input type="text" id="inputFirstName" ng-model="doctor.firstName" placeholder="First name"/>
</div>
</div>
<div>
<span class="nullable">
<select ng-model="user.lookupId" ng-options="select as speciality.lookupName for speciality in specialityList" ng-change="selectedSpecilaty()">
<option value="">-- choose speciality --</option>
</select>
</span>
</div>
</form>
</div>
我控制器
app.controller('UserCreationCtrl', ['$scope',
function ($scope) {
$scope.specialityList = [
{lookupId : 1, lookupName: 'speciality1'},
{lookupId : 2, lookupName: 'speciality2'},
{lookupId : 4, lookupName: 'speciality6'},
{lookupId : 3, lookupName: 'speciality3'}
];
$scope.user = {firstName: 'first name value', lookupId : 4};
$scope.selectedSpecilaty = function()
{
alert($scope.user.lookupId);
}
}]);
我怎樣才能做到這一點。警報框將該值標記爲「未定義」。
這是工作正是我需要感謝。 – 2014-09-19 14:21:29
在我的情況下(Angular 1.2.15)我有正確的表達方式,但我最後不必要地使用了「track by」表達式,這顯然會破壞它並導致模型跟蹤整個對象而不是值/ ID你正在選擇。這部分是由於缺少文檔,並可能在更新版本的Angular中糾正;請參閱GitHub問題:https://github.com/angular/angular.js/issues/6564。 – 2015-11-18 19:14:25
有關使用'as'定義在ngModel上設置的值的ngOptions官方文檔非常不清楚。感謝您使認爲更加清晰的兄弟! :) – 2017-10-26 18:06:10