作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在处理数据的函数完成后,我无法完成我的电子邮件代码。它是由 NG-click 按钮触发的,但通常的回调方法都不起作用。
Angular 将 post 请求发送到我的 express 服务器以进行邮寄。
function processQuote() {
$http({
method : 'POST',
url : '/send' ,
data : mailJson,
headers: { 'Content-type': 'application/json' }
}).success(function(data){
console.log('success!');
}).error(function(err,data){
console.log(err);
});
};
循环访问混合对象/数组表并返回 mailJson 数组,我将注入(inject)电子邮件
$scope.calculateAll = function(callback){
var mailJson = [];
var amount = 0;
$scope.contact.totalQuote = 0;
for (var i = 0; i < $scope.customers.length; i++) {
if( $scope.customers[i].area != 0) {
$scope.customers[i].total = parseInt($scope.customers[i].area, 10) * parseInt($scope.customers[i].price, 10);
$scope.contact.totalQuote += parseInt($scope.customers[i].total);
mailJson.push($scope.customers[i]);
}
}
mailJson.unshift($scope.contact);
callback(mailJson);
};
和 html,ng-click 即可激活。
<button type="button" ng-click="calculateAll(processform)" class="btn btn-primary">Submit</button>
最佳答案
你的问题有点令人困惑,但我知道你想在响应后运行 block 代码
$http 返回一个延迟的 promise ,所以你可以做的是
function processQuote() {
var promise=$http({
method : 'POST',
url : '/send' ,
data : mailJson,
headers: {
'Content-type': 'application/json'
});
return promise;
};
在你的 Controller 中
processQuote().then(function(){
//code should run after a response
});
关于javascript - 如何对 ng-click 激活的函数进行回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30475658/
我是一名优秀的程序员,十分优秀!