[英]How to set an option value in <md-option> of <md-select> from controller using 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.