Reputation: 2379
I want to make an item is pre selected in my selectbox,how it is possible in angularjs my code is given below
<select ng-model="selectedClient" ng-init="client.id == 117" ng-options="client.name for client in
clients" class="form-control"></select>
I want to make item 117 as preselected but it is not working.
Upvotes: 0
Views: 75
Reputation: 136144
Your mark up should look like below.
HTML
<select ng-model="selectedClient" ng-options="client.id as client.name for client in clients"
class="form-control" ng-init="selectedClient = 117"></select>
Upvotes: 0
Reputation: 555
Do not think AngularJS models as only values or ids if you have Arrays filled up objects. You have to approach it as object in this situation.
If you have an array like this:
var clients = [{
id: 1,
name: "Client 1",
active: false
}, {
id: 2,
name: "Client 2",
active: false
}, {
id: 3,
name: "Client 3",
active: false
}];
You can preselect one of them like below:
$scope.selectedClient = clients[0];
Keep in mind that the selectedClient is an object.
Upvotes: 1
Reputation: 388316
You assign the selected item to the model object selectedClient
$scope.selectedClient = $scope.clients[1]
Demo: Fiddle
Upvotes: 1