ng-options默认选中

 ng-options默认选中,很简单的问题,网上乱七八糟说不到重点,自己记录下常见的几种方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ng-options默认选中</title>
    <script src="api/angularjs/angular.min.js"></script>
</head>
<body>
    <div ng-app="myApp" ng-controller="myCtrl">
        <h5>使用ng-options,ng-init设默认值</h5>
        <select ng-init="selectedName = names[1]" ng-model="selectedName" ng-options="x for x in names"></select>
        <h5>使用ng-repeat</h5>
        <select ng-model="optionSelected">
            <option ng-repeat="x in names">{{x}}</option>
        </select>
        <h5>使用 ng-options,值为对象</h5>
        <select ng-model="selectSite" ng-options="y for (x,y) in sites"></select>
        <h5>选中的为{{selectSite}}</h5>
    </div>
</body>
 <script type="text/javascript">
     var app = angular.module('myApp',[]);
     app.controller("myCtrl",function ($scope) {
         $scope.names=['google','runoob','taobao'];
         $scope.sites ={
             site01:"google",
             site02:"runboo",
             site03:"taobao"
         };
         $scope.optionSelected=$scope.names[1];//为ng-repeat设默认值
         $scope.selectSite=$scope.sites.site02;//为ng-options设默认值
     })
 </script>
</html>

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值