javascript - 回调中的 Braintree 下降和 Angular $scope 不起作用

标签 javascript angularjs braintree

我正在尝试在 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/

相关文章:

javascript - 从使用 Express 的 Node JS 服务器导出一个变量,以便我可以在客户端访问它?

javascript - 从嵌套函数内的 forEach 循环中退出主函数

javascript - 如何换行javascript

javascript - 计算 ng-repeat 输入字段中的总和 AngularJS

paypal - 具有一对多关系的支付服务

javascript - jQuery 集合、功能和组织

javascript - 在使用 ngBindHtml 呈现的 html 片段上调用 ​​js 函数

javascript - 如何使用 Braintree 在客户端显示客户的付款方式?

Braintree & Taxamo 整合,如何做才对?

javascript - 指令的依赖