如何使用angularjs设置下拉默认值

时间:2016-03-02 09:43:07

标签: angularjs

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

任何人都可以帮助如何将默认选项值设置为“选择” 选择加载?

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>

7 个答案:

答案 0 :(得分:6)

我已更新您的JSFiddle here.

我使用options替换了ng-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.payment.type值初始化为'Select',为其指定了第一个$scope.options值。

答案 1 :(得分:1)

很简单。只需在选项内添加空值即可。

<option value="">Select</option> 

最后:

 <select name="type" ng-model="payment.type" ng-dropdown required ng-change="changeme()" >
            <option ng-option value="">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>

更新小提琴:http://jsfiddle.net/nq1eyj1v/118/

编辑1:

<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(payment)" ng-options="option.name for option in options"> 
              <option ng-option value="">Select</option>
         </select>
      </div>
   </div>

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

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

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

</div>

JS:

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


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

    $scope.changeme = function (payment) {
       alert(payment.name)
    }

}

FIDDLE:http://jsfiddle.net/nq1eyj1v/121/

答案 2 :(得分:0)

您必须在控制器中将payment.type设置为"Default"

myApp.controller( 'MyCtrl', function( $scope, ...) {
    $scope.payment = ...;
    $scope.payment.type = "Default";
    ...
}

答案 3 :(得分:0)

function MyCtrl($scope) {

$scope.payment.type="Default";
// payment.type = 'Check' ;
// $scope.changeme = function() {
// alert('here');

 }

通过此

修改您的js代码

答案 4 :(得分:0)

只需改变你的

<option ng-option value="Default">Select</option>

<option ng-option value="">Select</option>

答案 5 :(得分:0)

var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope) {
            $scope.payments= [{
                Id: 0,
                Name: 'Select'
            }, {
                Id: 1,
                Name: 'Cash'
            },{
                Id: 2,
                Name: 'Check'
            },{
                Id: 3,
                Name: 'Money Order'
            }];
        });
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
 <div ng-app="MyApp">
<div ng-controller="MyController">

<h2>Using ng-selected with payment id</h2><br>
    <select ng-model="drp">
        <option ng-repeat="payment in payments" value="{{payment.Id}}"
                ng-selected="{{ payment.Id== 0}}">
            {{payment.Name}}
        </option>
    </select>
    <br>
  <h2> Using ng-selected with payment Name</h2><br>
    <select ng-model="drp">
        <option ng-repeat="payment in payments" value="{{payment.Id}}"
                ng-selected="{{ payment.Name== 'Cash'}}">
            {{payment.Name}}
        </option>
    </select> 
</div>
</div>

答案 6 :(得分:0)

Following code set the default value of dropdown

In Html:
<select name="type" ng-model="payment.type" ng-dropdown required >
    <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>


In angular controller:

$scope.payment.type = 'Default';