默认选择的选项不适用于angularjs

时间:2017-01-01 04:43:46

标签: javascript angularjs angularjs-ng-repeat

我有一个观点

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

我在controller中执行此操作:

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

为什么它不起作用?

这是我的fiddle

1 个答案:

答案 0 :(得分:1)

尝试ng-options

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

controller

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

工作jsfiddle链接:

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

会为你效劳。