我使用$ 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
吗?我还没有找到任何参考文献。
答案 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();
});