如何在angularjs中设置select的默认选项

时间:2015-02-27 01:19:24

标签: angularjs

现在我正在尝试为选择下拉列表设置默认值。但是无法把它搞定,代码:

<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);            
                            }

我使用paymethodindex为select下拉列表设置默认值。我甚至可以在{{editselection.selectedmethod = Memethods [paymethodindex]}}的页面中打印出来,我认为索引部分工作正常,任何想法?

1 个答案:

答案 0 :(得分:1)

使用ng-selected默认所选选项,请参阅文档中的示例。

或者,使用ng-options代替ng-repeat来生成您的选项。