Angularjs $ q不回报承诺

时间:2016-12-26 03:17:41

标签: javascript angularjs

我正在尝试在Angularjs中调用服务方法。服务方法被称为正常,但是IT不会向在控制器中调用它的函数返回任何值。

如果有人能帮助我,我会很高兴的。这是我的代码吼叫。

  logosStreams.factory("processPaypal", ['$http', '$timeout', '$q',   '$state',function($http, $timeout, $q, $state){
  var transCall = {};
  var transcallPromise2 = $q.defer();   

  return {

    onSuccesfulPayment: function(payment) {
      console.log("payment success: " + JSON.stringify(payment, null, 4));
 transcallPromise2.resolve(payment);
    },
    onAuthorizationCallback: function(authorization) {
      console.log("authorization: " + JSON.stringify(authorization, null, 4));
      //return authorization;
    },
    createPayment: function(data) {
      // for simplicity use predefined amount
      var paymentDetails = new PayPalPaymentDetails("0", "0", "0");
      var payment = new PayPalPayment(data.amt, "USD", data.name, "Sale",
        paymentDetails);
      return payment;
    },

    buyInFutureBtn : function(e) {
        // future payment
        PayPalMobile.renderFuturePaymentUI(this.onAuthorizationCallback, this.onUserCanceled);
    },

    profileSharingBtn : function(e) {
        // profile sharing
        PayPalMobile.renderProfileSharingUI(["profile", "email", "phone","address", "futurepayments", "paypalattributes"],
         this.onAuthorizationCallback, this.onUserCanceled);
    },

    buyNowBtn : function(data) {

        // single payment
        PayPalMobile.renderSinglePaymentUI(this.createPayment(data), this.onSuccesfulPayment, this.onUserCanceled);
        return transcallPromise2.promise;

    },

    onPrepareRender: function() {

    },

    onUserCanceled: function(result) {
      console.log(result);
              transcallPromise2.reject(result);

    }

}

}])

在控制器内部调用buyNowBtn methond

  processPaypal.buyNowBtn($scope.MMParams).then(function(response){
            console.log(response);
  })

2 个答案:

答案 0 :(得分:0)

不返回价值的一个可能原因是您的承诺未得到解决。您的承诺只有在您致电onSuccessfulPayment()时才会得到解决。 您是否也可以将代码放在执行onSuccessfulPayment()函数的位置。

答案 1 :(得分:0)

您已在buyNow函数执行中返回了promise对象...尝试将promise作为返回所有函数连接的返回对象的一部分返回。