ID为angularjs NG-模式选择 [英] id as ng-model for select in angularjs

查看:258
本文介绍了ID为angularjs NG-模式选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到价值标签所选项目的期权价值的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-模型指向到上下文的特产 <$值C $ C> lookupId所

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

这篇关于ID为angularjs NG-模式选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆