【发布时间】:2018-09-11 18:20:32
【问题描述】:
背景
我正在尝试将重新验证码集成到使用 Angular 4 制作的单页应用程序中。我使用 grecaptcha.render(elt, {sitekey : 'XXX-my-public-key'}); 设置了站点密钥。
问题
当我调用grecaptcha.render() 时,我无法确定recaptcha js 是否已加载。因此,有时,我会收到此错误:
LoginComponent_Host.html:1 ERROR ReferenceError: grecaptcha is not defined
问题
在调用grecaptcha.render() 之前,如何确定重新验证码已完全加载?
以下是相关代码:
index.html
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
</html>
login.component.html
<div #captchaDiv class="m-t"></div>
login.component.ts
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit, AfterViewInit {
@ViewChild('captchaDiv')
captchaDiv: ElementRef;
[...]
ngOnInit(): void {
grecaptcha.render(this.captchaDiv.nativeElement, {sitekey : 'XXX-my-public-key'});
}
【问题讨论】:
标签: angular typescript recaptcha