javascript - angularjs 更新按钮文本基于 $http 响应 ng-click
问题描述
我正在尝试更新基于 $http 响应的单击按钮值。我想将按钮值更改为处理...同时使用 ng-click 功能发出 $http 请求。并且根据 Success/Error 响应,需要更新文本Success和Try Again等。
我正在使用ui-router,并且我有不同的表单元素分布在不同的状态,例如电子邮件、用户、密码等。
在我看来,我在电子邮件状态中遵循了代码。
<div ng-controller="RegisterEmailController">
<div class="form-group" style="position: relative;" ng-class="{ 'has-error': form.useremail.$dirty && form.useremail.$error.required && form.useremail.$invalid }">
<input type="email" name="useremail" id="useremail" ng-model="RegisterEmailController.userDetail.useremail" ng-required="true">
<span ng-show="form.useremail.$dirty && form.useremail.$error.required" class="help-block text-sm text-danger pl-4">Oops! Incorrect email address, or field is empty</span>
<span ng-hide="form.useremail.$dirty && form.useremail.$error.required" class="help-block d-block text-sm text-danger px-4">{{ErrorMsg}}</span>
</div>
<button type="button" ng-click="RegisterEmailController.nexStep()">{{buttonTxt}}</button>
</div>
在我的控制器中
var controller = this;
$scope.buttonTxt = 'Next Step';
controller.nexStep = function() {
$scope.buttonTxt = 'Processing...';
if ( validation_conditions ) {
$http({
method: "POST",
url: ApiURL,
headers: { "Content-Type": "application/json" },
data: getData
}).then(function success(response) {
$scope.buttonTxt = 'Success';
}, function error(response) {
$scope.buttonTxt = 'Try Again';
return;
});
} else {
$scope.buttonTxt = 'Try Again';
return;
}
}
使用上面的代码,我无法在单击时更改按钮文本,我也尝试过,controller.buttonTxt
但它在初始化时根本不打印值。我也尝试了 ng-bind指令,但没有运气。
请在我做错的地方帮助我。据我估计,这$scope
可能与控制器中的this
(var controller = this;) 冲突。
另外,如何根据响应将成功/错误类添加到输入父元素?
感谢您阅读我的查询,我提前感谢您的帮助。
解决方案
尝试这个
var controller = function ($http) {
var validation_conditions
var ApiURL = 'google.com'
var getData = ''
this.buttonTxt = 'Next Step';
this.nexStep = function() {
this.buttonTxt = 'Processing...';
if ( validation_conditions ) {
$http({
method: "POST",
url: ApiURL,
headers: { "Content-Type": "application/json" },
data: getData
}).then(function success(response) {
this.buttonTxt = 'Success';
}, function error(response) {
this.buttonTxt = 'Try Again';
return;
});
} else {
this.buttonTxt = 'Try Again';
return;
}
}
}
controller.$inject = ['$http']
angular
.module('app', [])
.controller('RegisterEmailController',controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="RegisterEmailController as ctrl">
<div class="form-group" style="position: relative;" ng-class="{ 'has-error': form.useremail.$dirty && form.useremail.$error.required && form.useremail.$invalid }">
<input type="email" name="useremail" id="useremail" ng-model="RegisterEmailController.userDetail.useremail" ng-required="true">
<span ng-show="form.useremail.$dirty && form.useremail.$error.required" class="help-block text-sm text-danger pl-4">Oops! Incorrect email address, or field is empty</span>
<span ng-hide="form.useremail.$dirty && form.useremail.$error.required" class="help-block d-block text-sm text-danger px-4">{{ctrl.ErrorMsg}}</span>
</div>
<button type="button" ng-click="ctrl.nexStep()">{{ctrl.buttonTxt}}</button>
</div>
推荐阅读
- kotlin - 强制为非空而不使用?
- c# - 是否有直接的方法将容器日志从物联网边缘设备发送到 Azure 日志分析工作区?
- winforms - Infragistics UltraToolbarsManager - 将 ApplicationDefaults 添加到 QuickAccessToolbar
- javascript - JavaScript:undefined++的结果是NaN,不是undefined?
- excel - 宏太慢
- ruby-on-rails - Rails,在设计登录存储位置重定向中保留锚标记
- php - 用 php 包含内容
- git - Github 无法从基础分支发出拉取请求
- java - 如何使用 json 对象从上下文处于 mainactivity 的对话框的 editText 发送数据?
- c# - 在实体框架事务中执行存储过程