admin管理员组

文章数量:1245083

I'm using $scope.$apply for callbacks, specifically with Stripe. Currently I have some code like

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);
    });
};

The problem with this is that I can't get any arguments to stripeCallback, namely response. Is there any way I can pass function arguments to $scope.$apply? I haven't been able to find any reference to this is the documentation.

I'm using $scope.$apply for callbacks, specifically with Stripe. Currently I have some code like

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);
    });
};

The problem with this is that I can't get any arguments to stripeCallback, namely response. Is there any way I can pass function arguments to $scope.$apply? I haven't been able to find any reference to this is the documentation.

Share Improve this question asked Jun 7, 2013 at 19:11 jclancyjclancy 52.4k5 gold badges31 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Wrap stripeCallback into an anonymous function:

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);
        });
    });
};

You can also just call $scope.$apply() after calling your callback:

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

本文标签: javascriptPassing function arguments to scopeapplyStack Overflow