id 作为 ng-model 在 angularjs 中选择 [英] id as ng-model for select in angularjs

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

问题描述

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

<form novalidate="novalidate" class="form-horizo​​ntal"><div class="control-group"><label class="control-label" for="inputFirstName">名字:</label><div class="控件"><input type="text" id="inputFirstName" ng-model="doctor.firstName" placeholder="名字"/>

<div><span class="nullable"><select ng-model="user.lookupId" ng-options="select as speciality.lookupName for specialityList" ng-change="selectedSpecilaty()"><option value="">--选择专业--</option></选择></span>

</表单>

我的控制器

app.controller('UserCreationCtrl', ['$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(){警报($scope.user.lookupId);}}]);

我该怎么做.将值显示为未定义"的警告框.

解决方案

ng-options 模式在开始时可能会令人困惑,因为有很多.

你现在写的意思是:

select as speciality.lookupName for specialityList 中的专业^-- 要设置的值 ^-- 显示什么 ^-- iteree 名称 ^-- iterables

因此,您在表达式中可用的变量是您在作用域上定义的所有变量,以及上下文 specialty 变量.当您选择某些内容时,它会将其分配给 select,这确实是 undefined.

您正在寻找的是

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

这将设置ng-model指向上下文specialtylookupId

的值

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>

My controller

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

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

解决方案

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

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.

What you're looking for is

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

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

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆