1

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

1
  • "$scope.selected_student = $scope.students[0];" writing this in your controller what is your goal? i mean that do you want to get student id as selected_student ? Commented Jan 1, 2017 at 5:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ng-option looks better for your requirement and it's binding student.name based on student.id. See for detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.