如何使用 <md-option> <md-select> AngularJS 在控制器中设置选项值?



我遵循相关的HTML代码:

<form name="bankDetailsForm" action="save" method="POST" ng-init="getBankDetails()">
  <md-input-container class="md-block" style="margin-top:0px;" flex=100> 
    <label>Transaction Type</label> 
    <md-select required ng-model="bankDetails.transactionType" ng-change="editField()"> 
      <md-option ng-repeat="type in transactionTypes" value="{{type}}">{{type}} 
      </md-option> 
    </md-select>
    <div ng-messages="bankDetailsForm.transactionType.$error">
      <div ng-message="required">Please select Transaction type</div>
    </div>
  </md-input-container>
</form>

相应的控制器代码如下:

var app = angular.module('app_name');
var isEmpty = true;
app.controller("bankDetailsController", [ "$scope", "$http", "config", "$mdToast", function($scope, $http, config, $mdToast) {
  $scope.bankDetails = {};
  $scope.transactionTypes = {
                NEFT : "NEFT",
                IMPS : "IMPS",
                WALLET : "WALLET",
               };
  $scope.getBankDetails = function() {
    $http.get(config.webServicesUrl.bankDetails, headerConfig).success(function(data) {
      if (data.body) {
        $scope.bankDetails.transactionTypes = $scope.transactionTypes.IMPS;
        isEmpty = false;
      }
    }).error(function(error) {
      console.log("Error : " + error.error);
    });
  };
}]);

我希望在表单加载时在事务类型的下拉列表中选择值"IMPS"。

我试过了,但我不能。那么有人可以更正我的代码以在下拉列表中预设值"IMPS"吗?

谢谢。

PS:请忽略ng-change="editField()"和其他变量。它们已在配置文件中定义,并且工作正常。所以请忽略它们。

这是一个

拼写错误。据此,ng-model="bankDetails.transactionType",您的选择期待transactionType但您正在填充transactionTypes

更改以下内容

$scope.bankDetails.transactionTypes = $scope.transactionTypes.IMPS;

$scope.bankDetails.transactionType = $scope.transactionTypes.IMPS;

最新更新