9

我正在使用 $scope.$apply 进行回调,特别是使用 Stripe。目前我有一些代码

var stripeCallback = function (status, response) {
    if (!response.error) {
        // do something
    } else {
        // do something else
    }
};

$scope.submit = function () {
    $scope.errorMessage = 'Processing...';
    $scope.buttonDisabled = true;
    // can't use bindings for some reason
    var myForm = $('#paymentform');
    Stripe.createToken(myForm, function (status, response) {
        $scope.$apply(stripeCallback);
    });
};

问题是我无法得到任何论据stripeCallback,即response. 有什么办法可以将函数参数传递给$scope.$apply?我还没有找到任何参考这是文档。

4

2 回答 2

7

包装stripeCallback成一个匿名函数:

var stripeCallback = function (status, response) {
    ....
};

$scope.submit = function () {
    $scope.errorMessage = 'Processing...';
    $scope.buttonDisabled = true;
    // can't use bindings for some reason
    var myForm = $('#paymentform');
    Stripe.createToken(myForm, function (status, response) {
        $scope.$apply(function() {
            stripeCallback(status, response);
        });
    });
};
于 2013-06-07T19:24:18.800 回答
5

您也可以$scope.$apply()在调用回调后调用:

Stripe.createToken(myForm, function (status, response) {
    stripeCallback(status, response);
    $scope.$apply();
});
于 2013-06-07T19:53:18.207 回答