如何在角度为ng-click激活的函数上进行回调?

时间:2022-03-02 19:45:23

I'm having trouble getting my email code to finish after the function processing it's data is finished. It's trigger by NG-click on a button but none of the usual callback methods are working.

在函数处理完成后,我无法完成电子邮件代码的处理。通过NG单击按钮触发它,但通常的回调方法都不起作用。

Angular sends post requests to my express server to be mailed.

Angular将邮件请求发送到我的快递服务器以进行邮寄。

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);
  }); 
};  

loops through mixed object/array table and returns mailJson array I will pump into email

循环通过混合对象/数组表并返回mailJson数组我将泵入电子邮件

$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);
};

and the html, ng-click to activate.

和html,ng-click激活。

<button type="button" ng-click="calculateAll(processform)" class="btn btn-primary">Submit</button>

1 个解决方案

#1


1  

Your question was little confusing but I got that you want to run block code after response

你的问题有点令人困惑,但我知道你想在响应后运行块代码

$http return a deferred promise so what you can do is

$ http返回延期保证,所以你能做的就是

function processQuote() {
var promise=$http({
  method  : 'POST',
  url     : '/send' ,
  data    : mailJson,
     headers: {
    'Content-type': 'application/json'
});
return promise;
};  

In your controller

在你的控制器中

processQuote().then(function(){
//code should run after a response
});

#1


1  

Your question was little confusing but I got that you want to run block code after response

你的问题有点令人困惑,但我知道你想在响应后运行块代码

$http return a deferred promise so what you can do is

$ http返回延期保证,所以你能做的就是

function processQuote() {
var promise=$http({
  method  : 'POST',
  url     : '/send' ,
  data    : mailJson,
     headers: {
    'Content-type': 'application/json'
});
return promise;
};  

In your controller

在你的控制器中

processQuote().then(function(){
//code should run after a response
});