当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > Angular中ng-options下拉数据默认值的设定方法

Angular中ng-options下拉数据默认值的设定方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

今天学习了一下angular中ng-options下拉数据默认值的设定方法,留个笔记

直接上代码

    <div class="form-group">
        <label class="col-sm-2 control-label">教师</label>
             <div class="col-sm-10">
              <select style="display:block;
                  width:100%;
                  height:34px;
                  padding:6px 12px;
                  font-size:14px;
                  line-height:1.4;
                  border-radius:4px;
                  border:1px solid #ccc;
                  color:#555; "
                  ng-model="editcourse.teachername"
                  ng-options="teacher.username for teacher in teacherlist" required>
                <option value="">选择教师</option>
               </select>
             </div>
      </div>

angularjs

    //data为课程的编号id
   $scope.edit = function (data) {
      //通过课程id获取课程对象
      courseservice.getbycourseid(data).then(function (result) {
        $scope.editcourse = result.data;
        //默认值设定
        //先通过课程里面的教师id获取教师对象
        courseservice.getteacherbyteacherid(result.data.teacherid).then(function (result) {
         //$scope.teacherlist为所有教师的列表
          for (i = 0; i < $scope.teacherlist.length; i++) {
            //如果当前课程教师的id与当前遍历到的教师的id相等的话就把当前遍历到的这个教师的对象给到 ng-model="editcourse.teachername"
            if (result.data.userid == $scope.teacherlist[i].userid) {
              $scope.editcourse.teachername = $scope.teacherlist[i];
            }
          }
        });
        angular.element("#edit").modal({
          show: true
        })
      })
    }


演示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网