linyuanxie
linyuanxie

Reputation: 787

How to set default option for select in angularjs

Now I am trying to set a default value for a select dropdown. but couldn't get it down, code:

<div class='paymentmethods subcontent' ng-switch on="editpaymentmethod">{{editselection.selectedmethod=Memethods[paymethodindex]}}// This gives me what I need,but select doesn't. confused?
            <select ng-model='editselection.selectedmethod' ng-init="editselection.selectedmethod=Memethods[paymethodindex]" id='methods' ng-switch-when='Y'>
              <option ng-repeat="method in Memethods" value="{{method}}">{{method}}</option>            
            </select>
            <select ng-model='editselection.selectedmethod' ng-init="editselection.selectedmethod=companies[paymethodindex]" ng-switch-when='N' style='float:left'>
              <option ng-repeat='companyoption in companies' value="{{companyoption}}">{{companyoption}}</option>
            </select>
            </div>

JS:

$scope.paymethodindex = $scope.Memethods.indexOf(paymethod);
            console.log($scope.paymethodindex);
            if(reimbursement=='N')
                            {
            $scope.paymethodindex = $scope.companies.indexOf(paymethod);            
                            }

I use paymethodindex to set default value for select dropdown. I can even print out in the page with {{editselection.selectedmethod=Memethods[paymethodindex]}}, I think the index part works fine, any idea?

Upvotes: 0

Views: 56

Answers (1)

floribon
floribon

Reputation: 19193

Use ng-selected to default the selected option, see example in the doc.

Alternatively, use ng-options instead of ng-repeat to generate your options.

Upvotes: 1

Related Questions