如何从Angularjs的Dropdownlist中获取选定值? [英] how to get Selected Value from Dropdownlist in Angularjs?

查看:94
本文介绍了如何从Angularjs的Dropdownlist中获取选定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用json中的angular js填充了我的下拉列表.但是选择不起作用.如何选择下拉列表项?

i filled my dropdownlist by angular js from json. But selection is not working. How can i select dropdownlist item?

TaskTypeDefinitionController.js:

TaskTypeDefinitionController.js:

 var roleList = [{"id" :1, "value" :"test" },{"id" : 2, "value" : "test2"}];
        roleList.then(function (data) {
            $scope.roleList = data;
            var jsonArr = [];
            for (var i = 0; i < data.length; i++) {

                jsonArr.push({
                    id: data[i].roleName.serno,
                    value: data[i].roleName.value
                });
            }
            $scope.Names = jsonArr;
            console.log(jsonArr);
        });

html(View):

html(View) :

<select class="form-control" placeholder="Görev Tipi Adı" ng-
options="field.id as field.value for field in Names" ng-model="Name"  
required></select>

推荐答案

选择不起作用意味着您需要设置下拉列表的默认值.如果是,请尝试使用下面的代码代替您的代码.

selection is not working means you need to set the default value of dropdown. If yes then try this below code instead of your code.

 var roleList = [{"id" :1, "value" :"test" },{"id" : 2, "value" : "test2"}];
        roleList.then(function (data) {
            $scope.roleList = data;
            var jsonArr = [];
            for (var i = 0; i < data.length; i++) {

                jsonArr.push({
                    id: data[i].roleName.serno,
                    value: data[i].roleName.value
                });
            }
             jsonArr.push({id:0,value:"Select Name"});
             $scope.Name=jsonArr[jsonArr.length-1];
            $scope.Names = jsonArr;
            console.log(jsonArr);
        });

解释

Name是下拉菜单的型号名称.这是您的第一次undefined.因此,我认为它可能显示为空白.因此,您需要为Name对象分配一个值.在我的代码中给Name对象分配了一个值(选择名称).

Explain

The Name is your model name of dropdown. It is undefined on your first time. So I think it's may be showing blank. So you need to assign a value to the Name object. In my code have assign a value(Select Name) to the Name Object.

在此代码中

         jsonArr.push({id:0,value:"Select Name"});
         $scope.Name=jsonArr[jsonArr.length-1];

这篇关于如何从Angularjs的Dropdownlist中获取选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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