Krish
Krish

Reputation: 517

how to set dropdown default value using angularjs

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>

Upvotes: 4

Views: 48737

Answers (7)

Uttam Kar
Uttam Kar

Reputation: 323

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';

Upvotes: 0

Erazihel
Erazihel

Reputation: 7605

I've update your JSFiddle here.

I replaced the options using 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];
}

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

Upvotes: 10

Tripat Bala Singh
Tripat Bala Singh

Reputation: 1

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>

Upvotes: 0

Akshay Kumar
Akshay Kumar

Reputation: 549

Just Change Your

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

to

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

Upvotes: 0

Silverspur
Silverspur

Reputation: 923

You have to set payment.type to "Default" in your controller.

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

Upvotes: 0

Ved
Ved

Reputation: 12093

Very easy. Just add empty value inside option.

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

Finally :

 <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>

Updated Fiddle: http://jsfiddle.net/nq1eyj1v/118/

EDIT 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/

Upvotes: 1

Hassan Tariq
Hassan Tariq

Reputation: 740

function MyCtrl($scope) {

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

 }

Modify your js code by this

Upvotes: 0

Related Questions