Thian Kian Phin
Thian Kian Phin

Reputation: 931

Default selected option doesn't work in angularjs

I have a view

<select ng-model="selected_student" class="form-control">
    <option ng-repeat="obj in students" value="{{obj.id}}">{{obj.name}}</option>
</select>

I am doing this in controller:

$scope.selected_student = $scope.students[0];

why it doesn't work?

Here's my fiddle

Upvotes: 1

Views: 60

Answers (1)

Avnesh Shakya
Avnesh Shakya

Reputation: 3906

Try ng-options:

 <select ng-model="selected_student" class="form-control" ng-options="student.id as student.name for student in students">

And in controller:

$scope.selected_student = $scope.students[0].id;

Working jsfiddle link:

https://jsfiddle.net/avnesh2/boye4ezk/

Will work for you.

Upvotes: 1

Related Questions