简体   繁体   中英

How to set default value to <select> in Angularjs?

I have two selects one for classes and another for teachers as follows,

    <label for="">Select Class</label><br> 
    <select ng-model="currentClass" ng-options="class as class.name for class in classes" ng-change="listTeachers()">
    <option value="">Select Class</option>
</select>  


    <label for="">Select Teacher</label><br> 
        <select ng-model="currentTeacher" ng-options="teacher as teacher.name for teacher in teachers" ng-change="showStudents()">
        <option value="">Select Teacher</option>
    </select>
  1. My problem is, if choose class and then choose teacher it's fine, but after choosing teacher if again I choose another class then I want to set 2nd select a value as "select teacher", how to do it?

  2. Also tell me, is there any way of setting default value instead of setting it through option tag ?

in your controller on the listTeachers() function you need to set the model on the value you desire, example:

vm.listTeachers = function() {
    vm.currentTeacher = '';
    ... // rest of your code 
}

so when ever you change your class it invokes listTeacher, and i will reset the value, hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM