将函数参数传递给$ scope。$ apply?

时间:2013-06-07 19:11:37

标签: javascript angularjs callback

我使用$ scope。$适用于回调,特别是使用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吗?我还没有找到任何参考文献。

2 个答案:

答案 0 :(得分: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);
        });
    });
};

答案 1 :(得分:5)

您也可以在致电回拨后致电$scope.$apply()

Stripe.createToken(myForm, function (status, response) {
    stripeCallback(status, response);
    $scope.$apply();
});