javascript - Angular 的 $scope 在 PayPal 方法 promise 中不起作用

标签 javascript angularjs paypal paypal-sandbox

在 PayPal promise 中,我的 $scope 似乎被破坏了:

onAuthorize: function(data, actions) {

  paypal.request.post(execute_payment_url)
    .then(function(data) {

      $rootScope.loader = true; // no response?!

      if (data.payment.state === 'approved') {
        alert('hey!') // worked

        $location.path('/signup/').search({ // no response
          email: data.payment.payer.payer_info.email
        });
      }

    })
    .catch(function(err) {
      alert('error')
    }).finally(function() {
      $rootScope.loader = false;
    })
}

我不知道为什么,$rootscope$location 已经被注入(inject)到我的 Controller 中。当我调用 console.log($location) 时,我可以看到对象就在那里。

最佳答案

发生这种情况是因为 PayPal promise 不是 AngularJS $q 服务 promise 。 $q 服务 promise 与 AngularJS 框架摘要循环集成。可以用$q.when将其带入$q服务 promise 事件队列。

onAuthorize: function(data, actions) {

  //paypal.request.post(execute_payment_url)
  $q.when(paypal.request.post(execute_payment_url))
    .then(function(data) {

      $rootScope.loader = true; // no response?!

      if (data.payment.state === 'approved') {
        alert('hey!') // worked

        $location.path('/signup/').search({ // no response
          email: data.payment.payer.payer_info.email
        });
      }

    })
    .catch(function(err) {
      alert('error')
    }).finally(function() {
      $rootScope.loader = false;
    })
}

$q.when

Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted.

-- AngularJS $q Service API Reference - $q.when .

关于javascript - Angular 的 $scope 在 PayPal 方法 promise 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41287769/

相关文章:

javascript - Ajax 只播放音频文件一次

javascript - 在 HTML 中的超链接事件上显示文本文件

float div 的 CSS3 动 Canvas 局更改

javascript - ng-repeat 执行多次

javascript - anchor 标记不适用于 Angular UI-Router

php - Paypal PHP Cronjob

javascript - 根据多个下拉选择隐藏/显示行(过滤)

javascript - JavaScript 中的整数

wordpress - 取消 Paypal 沙盒定期付款

c# - 如何将 PayPal 与 ASP.NET 集成?