id为ng-model,用于在angularjs中选择

Pra*_*ddy 10 angularjs ng-repeat

我没有得到所选标签项的选项值的id值.

<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>
Run Code Online (Sandbox Code Playgroud)

我的控制器

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);
    }
}]);    
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点.警告框将值显示为"未定义".

Phi*_*ret 24

ng-options 模式可能会让人感到困惑,因为有相当多的模式.

你现在写的是什么意思:

select     as     speciality.lookupName    for   speciality   in   specialityList
 ^-- the value to set to   ^-- what to display      ^-- iteree name  ^-- iterables
Run Code Online (Sandbox Code Playgroud)

因此,表达式中可用的变量是您在范围和上下文specialty变量上定义的所有变量.当你选择它会分配给它时select,确实如此undefined.

你在寻找什么

ng-options="speciality.lookupId as speciality.lookupName for speciality in specialityList"
Run Code Online (Sandbox Code Playgroud)

这将设置ng-model指向上下文specialty的值lookupId