我有一个联系表单,在提交时,它会向后端文件发送一个POST:
<div class="field text-center">
<button type="submit" class="submit form-btn" ng-disabled="contactForm.$invalid">Send</button>
</div>
POST:
$scope.processForm = function() {
$http({
method : 'POST',
url : '/php/contact.php',
data : $.param($scope.contactData), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
}).then(function successCallback(response) {
}, function errorCallback(response) {
});
}
我希望在功能成功POST时以及出现错误时更改按钮的颜色和文本。我的猜测是,在成功和错误回调函数中,我必须更新DOM,但我该怎么做?或者有更好/更简单的方法吗?
答案 0 :(得分:1)
您可以使用ngStyle
更改控制器中DOM元素的样式。
HTML:
<div class="field text-center">
<button ng-style="httpCall" type="submit" class="submit form-btn" ng-disabled="contactForm.$invalid">{{httpCallText}}</button>
</div>
JS:
$scope.processForm = function() {
$http({
method : 'POST',
url : '/php/contact.php',
data : $.param($scope.contactData), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
}).then(function successCallback(response) {
var buttonColor = "green";
$scope.httpCallText = "Success";
$scope.httpCall = {
'background-color': buttonColor
}
}, function errorCallback(response) {
var buttonColor = "red";
$scope.httpCallText = "Error";
$scope.httpCall = {
'background-color': buttonColor
}
});
}
如果您对此答案有任何疑问,请与我们联系!