加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular 的 select 中 ng-options 设置默认值

发布时间:2020-12-17 09:47:30 所属栏目:安全 来源:网络整理
导读:angular 的 select 中 ng-options 设置默认值 在 AngularJS 开发的后台 web系统 中,经常需要对后台数据进行修改,而在修改数据的过程中,若页面中有 select 下拉框,需要让其选中原来的数据,即 select 中需要设置默认值。 默认值设置方法: 在 select 中使

angular 的 select 中 ng-options 设置默认值

AngularJS 开发的后台web系统中,经常需要对后台数据进行修改,而在修改数据的过程中,若页面中有select下拉框,需要让其选中原来的数据,即select中需要设置默认值。

默认值设置方法:

  1. select中使用ng-model绑定数据myColor

  2. 在对应的controller中定义ng-model全局数据$scope.myColor,并为其赋值,该值即为select的默认值。

代码如下:

angular.module('myApp',[])
  .controller('MyController',['$scope'],function($scope) {
    $scope.colors = ['black','white','red','blue','yellow'];
    
    $scope.myColor = $scope.colors[2];
  })
<div ng-controller="MyController">
  <select ng-model="myColor" ng-options="color for color in colors"></select>
</div>

显示效果:

说明: 该文档参考angular官方api

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读