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

jtoj6r0c  于 2022-10-31  发布在  Angular
关注(0)|答案(1)|浏览(260)

我有以下相关的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”?

  • 谢谢-谢谢

P.S.:请忽略ng-change=“editField()”和其他变量。它们已在配置文件中定义,并且运行正常。因此请忽略它们。

f3temu5u

f3temu5u1#

这是一个打字错误。根据这个,ng-model="bankDetails.transactionType",你的选择应该是transactionType,但是你填充的是transactionTypes
更改以下内容

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

结束日期

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

相关问题