Home>
Today I learned how to set the default value of the ng-options drop-down data in angular.Leave a note
Directly on the code
<div>
<label>teacher</label>
<div>
<select
ng-model="editcourse.teachername"
ng-options="teacher.username for teacher in teacherlist" required>
<option value="">select teacher</option>
</select>
</div>
</div>
angularjs
//data is the id of the course
$scope.edit=function (data) {
//Get course object by course id
courseservice.getbycourseid (data) .then (function (result) {
$scope.editcourse=result.data;
//Default value setting
//First get the teacher object by the teacher id in the course
courseservice.getteacherbyteacherid (result.data.teacherid) .then (function (result) {
//$scope.teacherlist is a list of all teachers
for (i=0;i<$scope.teacherlist.length;i ++) {
//If the id of the teacher of the current course is equal to the id of the teacher currently traversed, give the object of the teacher currently traversed to ng-model="editcourse.teachername"
if (result.data.userid == $scope.teacherlist [i] .userid) {
$scope.editcourse.teachername=$scope.teacherlist [i];
}
}
});
angular.element ("#edit"). modal ({
show:true
})
})
}
Demo
Related articles
- angularjs select assignment ng-options configuration method
- AngularJS basic ng-model-options directive simple example
- Data binding method of ng-options in AngularJS to implement drop-down list
- How to use ng-options in angular instruction notes
- AngularJS ng-model instance code for dynamically binding ng-options
- Explain how to set the default value (initial value) when using angularjs's ng-options
- Ng-options directive for AngularJS study notes
- Method for setting data synchronization in ng-model-options in angularJs
Trends