如何使用.then方法为vcRecaptchaService.execute()?

时间:2017-11-02 03:47:53

标签: javascript angularjs promise recaptcha invisible-recaptcha

我正在使用anuglar-recaptcha将Google Invisible Recaptcha添加到我的登录表单

我面临的问题是,在我调用vcRecaptchaService.execute之后,如果我打印recaptcha_reponse,我会看到undefined作为输出。但是如果我使用测试函数recaptcha_reponse几秒后打印printRecaptchaReponse,我会看到recaptcha_reponse被打印出来。

这里的问题是,库函数vcRecaptchaService.execute()在这里没有返回$promise。它实际上返回none

有没有办法宣传这个?

问题是什么:

// 1. Call this function from html first
$scope.testRecaptchaExecute = function() {
    vcRecaptchaService.execute($scope.widgetId);
    console.log($scope.user.recaptcha_reponse);
    // undefined :-(
    return();
}

// 2. Call this function from html few seconds after testRecaptchaExecute() is called

$scope.printRecaptchaReponse = function() {
    console.log($scope.user.recaptcha_reponse);
    //JVRk4cZ4J_RluoH2
}

我想要什么: 像,

vcRecaptchaService.execute().then(ret => { //do something here }) 此代码的问题是vcRecaptchaService.execute不返回承诺。此代码无效

问题摘要 如何使用.then vcRecaptchaService.execute()方法?

1 个答案:

答案 0 :(得分:2)

您可以尝试recaptcha

的此工作代码

的Javascript

$scope.captcha = {
    response: null,
    widgetId: null,
    modelKey: 'test key',
    size: 'normal',
    setResponse: function (response) {
        $scope.captcha.response = response;
    },
    setWidgetId: function (widgetId) {
        $scope.captcha.widgetId = widgetId;
    },
    cbExpiration: function () {
        vcRecaptchaService.reload($scope.captcha.widgetId);
        $scope.captcha.response = null;
    }
}

HTML

<div ng-model="form.recaptcha" 
     size="captcha.size" 
     vc-recaptcha theme="'light'" 
     key="captcha.modelKey" 
     on-create="captcha.setWidgetId(widgetId)"
     on-success="captcha.setResponse(response)" 
     on-expire="captcha.cbExpiration()" 
     required>
</div>

<p ng-show="!form.recaptcha" class="error">Please resolve the captcha</p>