如何使用angularjs设置下拉菜单默认值 [英] how to set dropdown default value using angularjs

查看:283
本文介绍了如何使用angularjs设置下拉菜单默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考下面的小提琴 http://jsfiddle.net/nq1eyj1v/117/

please refer the below fiddle http://jsfiddle.net/nq1eyj1v/117/

有谁能帮助您将默认选项值设置为选择" 选择加载吗?

can any one help how to set default option value as "Select" Select on load ?

HTML:

<div ng-app="myApp" ng-controller="MyCtrl">
   <div class="col-xs-12">
      <label class="col-xs-6 control-label">Type:</label>
      <div class="col-xs-6">
         <select name="type" ng-model="payment.type" ng-dropdown required ng-change="changeme()" >
            <option ng-option value="Default">Select</option>
            <option ng-option value="Cash">Cash</option>
            <option ng-option value="Check">Check</option>
            <option ng-option value="Money Order">Money Order</option> 
         </select>
      </div>
   </div>

   <div class="col-xs-12"  ng-if="payment.type == 'Cash'">
     11111111111111
    </div>

    <div class="col-xs-12" ng-if="payment.type == 'Check'">
    22222222222
    </div>

    <div class="col-xs-12"  ng-if="payment.type == 'Money Order'">
     3333333333333
    </div>

</div>

推荐答案

我在这里更新了您的JSFiddle .

I've update your JSFiddle here.

我用ng-options代替了options.

HTML

<div ng-app="myApp" ng-controller="MyCtrl">
    <div class="col-xs-12">
        <label class="col-xs-6 control-label">Type:</label>
            <div class="col-xs-6">
                <select name="type" ng-model="payment"
                    ng-dropdown required 
                    ng-change="changeme()"
                    ng-options="option.type for option in options">
                </select>
            </div>
        </div>

        <div class="col-xs-12"  ng-if="payment.type == 'Cash'">
            11111111111111
        </div>

        <div class="col-xs-12" ng-if="payment.type == 'Check'">
            22222222222
        </div>

        <div class="col-xs-12" ng-if="payment.type == 'Money Order'">
            3333333333333
        </div>
    </div>
</div>

JS

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.options = [
        { type: 'Select' },
        { type: 'Cash' },
        { type: 'Check' },
        { type: 'Money Order' }
    ];

    $scope.payment = $scope.options[0];
}

如您所见,我通过将第一个$scope.options值分配给'Select'并将其初始化为'Select'.

As you can see, I initialized the $scope.payment.type value as 'Select' by assigning it the first $scope.options value.

这篇关于如何使用angularjs设置下拉菜单默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆