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