我正在尝试在 Angular Controller 中使用 UI 中的 braintree drop,例如。
https://jsfiddle.net/6jmkwode/
function PaymentCtrl($scope) {
$scope.hasCalledBack = 'Nope';
braintree.setup('BRAINTREE_KEY',
'dropin', {
container: 'dropin',
onPaymentMethodReceived: function (obj) {
$scope.hasCalledBack = 'YEP!';
alert('Did the scope variable change? No? Well it should have....');
}
});
}
但是 $scope.hasCalledBack 永远不会在回调中改变,即使警报触发也是如此。
最佳答案
简单地用$scope.$apply() 包裹你的回调代码(很好 article 关于它):
...
onPaymentMethodReceived: function (obj) {
$scope.$apply(function() {
$scope.hasCalledBack = 'YEP!';
alert('Did the scope variable change? Yes!');
});
}
...
$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.
查看更新 Demo .
关于javascript - 回调中的 Braintree 下降和 Angular $scope 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841185/