0

I have a dropdown list:-

<label>User Code:</label>
<select ng-options="c as c.User for c in userList"
    ng-model="selectedUser" id="search3">
</select>

The data in this dropdown is coming from an API. My directive code:-

scope.getAllPeriodicities = function(){
    scope.userList = [];
    ApiServices.getAllPeriodicities().then(function (response) {
        scope.userList = response.data;
    });
}

scope.getAllPeriodicities();

There is an User Code:-SS1234. I want this user to be selected whenever the page loads. I am thinking of using selected attribute but not sure how to use it on the fetched data.

Erazihel
  • 7,295
  • 6
  • 30
  • 53
shreya gupta
  • 141
  • 12

4 Answers4

1
scope.getAllPeriodicities = function(){
  scope.userList = [];
    ApiServices.getAllPeriodicities().then(function (response) {
       scope.userList = response.data;
       scope.selectedUser = "SS1234";
   });
}
scope.getAllPeriodicities();
n1kkou
  • 3,096
  • 2
  • 21
  • 32
1

Set your default value to the ng-model by using ng-init or ng-value

<select ng-options="c as c.User for c in userList" ng-model="selectedUser" 
 id="search3" ng-value={{selectedUser = 'SS1234'}}></select>

//or

<select ng-options="c as c.User for c in userList" ng-model="selectedUser" 
 id="search3" ng-init="selectedUser = 'SS1234'"></select>
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
1

simply asign, scope.selectedUser="SS1234" user you want.

scope.userList = response.data;
scope.selectedUser = scope.userList[0].User /* here i choose 0th index of user. you can change as you want */
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

In your controller add,

$scope.selectedUser = 'SS1234';
Gokulan P H
  • 1,838
  • 1
  • 10
  • 13