diff --git a/lib/captcha.component.ts b/lib/captcha.component.ts index 8a247e3..94b1fff 100644 --- a/lib/captcha.component.ts +++ b/lib/captcha.component.ts @@ -5,7 +5,8 @@ import { Output, EventEmitter, NgZone, - ViewChild, ElementRef, forwardRef + ViewChild, ElementRef, forwardRef, + AfterViewInit } from '@angular/core'; import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms'; import { ReCaptchaService } from './captcha.service'; @@ -21,7 +22,7 @@ import { ReCaptchaService } from './captcha.service'; } ] }) -export class ReCaptchaComponent implements OnInit, ControlValueAccessor { +export class ReCaptchaComponent implements OnInit, AfterViewInit, ControlValueAccessor { @Input() site_key: string = null; @Input() theme = 'light'; @@ -50,26 +51,29 @@ export class ReCaptchaComponent implements OnInit, ControlValueAccessor { } ngOnInit() { + } + + ngAfterViewInit(): void { this._captchaService.getReady(this.language, this.global) - .subscribe((ready) => { - if (!ready) - return; - // noinspection TypeScriptUnresolvedVariable,TypeScriptUnresolvedFunction - this.widgetId = (window).grecaptcha.render(this.targetRef.nativeElement, { - 'sitekey': this.site_key, - 'badge': this.badge, - 'theme': this.theme, - 'type': this.type, - 'size': this.size, - 'tabindex': this.tabindex, - 'callback': ((response: any) => this._zone.run(this.recaptchaCallback.bind(this, response))), - 'expired-callback': (() => this._zone.run(this.recaptchaExpiredCallback.bind(this))) - }); - setTimeout(() => { - this.loaded.emit(true); - }, 0); + .subscribe((ready) => { + if (!ready) + return; + // noinspection TypeScriptUnresolvedVariable,TypeScriptUnresolvedFunction + this.widgetId = (window).grecaptcha.render(this.targetRef.nativeElement, { + 'sitekey': this.site_key, + 'badge': this.badge, + 'theme': this.theme, + 'type': this.type, + 'size': this.size, + 'tabindex': this.tabindex, + 'callback': ((response: any) => this._zone.run(this.recaptchaCallback.bind(this, response))), + 'expired-callback': (() => this._zone.run(this.recaptchaExpiredCallback.bind(this))) }); - } + setTimeout(() => { + this.loaded.emit(true); + }, 0); + }); + } // noinspection JSUnusedGlobalSymbols public reset() {