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

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

我有以下相关的HTML代码:

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

相应的控制器代码如下:

  1. var app = angular.module('app_name');
  2. var isEmpty = true;
  3. app.controller("bankDetailsController", [ "$scope", "$http", "config", "$mdToast", function($scope, $http, config, $mdToast) {
  4. $scope.bankDetails = {};
  5. $scope.transactionTypes = {
  6. NEFT : "NEFT",
  7. IMPS : "IMPS",
  8. WALLET : "WALLET",
  9. };
  10. $scope.getBankDetails = function() {
  11. $http.get(config.webServicesUrl.bankDetails, headerConfig).success(function(data) {
  12. if (data.body) {
  13. $scope.bankDetails.transactionTypes = $scope.transactionTypes.IMPS;
  14. isEmpty = false;
  15. }
  16. }).error(function(error) {
  17. console.log("Error : " + error.error);
  18. });
  19. };
  20. }]);

我希望在表单加载时,在交易类型下拉列表中选择值“IMPS”。
我试过了,但我不能。所以有人可以请纠正我的代码,以便在下拉菜单中预设值“IMPS”?

  • 谢谢-谢谢

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

f3temu5u

f3temu5u1#

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

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

结束日期

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

相关问题