简体   繁体   中英

using ng-init in dynamic dropdownlist angularjs

I am creating a web app using mvc5 with angularjs,

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,

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

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 )

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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