簡體   English   中英

我如何在ng-click中激活Angular激活的函數進行回調?

[英]How do i make callbacks on functions activated by ng-click in angular?

函數處理完數據后,我無法完成我的電子郵件代碼。 它是通過NG單擊按鈕觸發的,但沒有任何常用的回調方法起作用。

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

循環通過混合對象/數組表並返回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);
};

然后html,ng-click激活。

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

您的問題有點令人困惑,但我得到您想要在響應后運行塊代碼

$ http返回一個延遲的承諾,所以您可以做的是

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

在您的控制器中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM