其他分享
首页 > 其他分享> > 回调中的Braintree Drop和Angular $scope不起作用

回调中的Braintree Drop和Angular $scope不起作用

作者:互联网

我正在尝试利用角度控制器内UI中的Braintree下降.

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.

标签:braintree,angularjs,javascript
来源: https://codeday.me/bug/20191120/2043911.html