ID为angularjs NG-模式选择模式、ID、angularjs、NG

2023-09-13 05:04:19 作者:惊、天地无波

我没有得到价值标签所选项目的期权价值的id。

I am not getting the id value of the option value of selected item of tag.

<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);
    }
}]);    

我怎样才能做到这一点。警告框displying价值为未定义。

How can i do this. the alert box displying the value as 'undefined'.

推荐答案

NG-选项模式可能会造成混淆上手,因为有相当多的一群。

ng-options patterns can be confusing to get started with, as there are quite a bunch.

什么你写现在的意思是:

What you wrote right now means:

select     as     speciality.lookupName    for   speciality   in   specialityList
 ^-- the value to set to   ^-- what to display      ^-- iteree name  ^-- iterables

所以,你必须在EX pression可用的变量是对一切都很您已经在范围界定,以及上下文特种变量。当你选择的东西,它会其分配给选择,这确实是未定义

So the variables you have available in the expression are everythin you have defined on the scope, and the contextual specialty variable. When you select something it'll assign it to select, which is indeed undefined.

什么你要找的是

ng-options="speciality.lookupId as speciality.lookupName for speciality in specialityList"

这将设置什么 NG-模型指向到上下文的特产 lookupId所

This will set the value of what ng-model is pointing to to the contextual specialty's lookupId