简体   繁体   English

在动态下拉列表angularjs中使用ng-init

[英]using ng-init in dynamic dropdownlist angularjs

I am creating a web app using mvc5 with angularjs, 我正在使用带有angularjs的mvc5创建一个Web应用程序,

i have a dynamic dropdownlist, i am fetching my data from my database, 我有一个动态下拉列表,我正在从数据库中获取数据,

my dropdownlist look like the following, 我的下拉列表如下所示,

<select ng-model="mduser.dataindrop" class="form-control" ng-options="x as x.username for x in dataindrop" ng-change="change(mduser.dataindrop)"></select>

all is working fine except one thing, 除了一件事情,一切都很好

i am not able to set initial value in my dropdownlis, 我无法在我的下拉菜单中设置初始值,

i want to set ( select ) as an initial value, but not able to succeed yet, 我想将( select )设置为初始值,但还不能成功,

this is how my controller looks, 这就是我的控制器的外观,

$scope.usernameindrop = function () {
                $http.get('/Web Service/dashboard.asmx/getusername', {
                    params: {
                        userid: $sessionStorage.userautoid
                    }
                }).then(function(response){
                    $scope.dataindrop = response.data;
                    console.log(response.data);
                })
            }

and this is my data in json 这是我在json中的数据

Array[3]
0
:
Object
$$hashKey
:
"object:23"
userid
:
"2f8e8bac-a631-4df3-8ab9-b0c293099bc6"
username
:
"new"
__proto__
:
Object
1
:
Object
$$hashKey
:
"object:24"
userid
:
"8ba0ae53-078d-4301-95eb-409acb1c92c6"
username
:
"b"

i just want to set ( select as an initial value for my dropdownlist ) 我只想设置( select as an initial value for my dropdownlist

Try like this 这样尝试

<select ng-model="mduser.dataindrop"
 ng-options="x.userid as x.username for x in dataindrop"></select>

and in controller init model as this 并在控制器初始化模型中这样

$scope.mduser.dataindrop  = dataindrop[0].userid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM