From c6319fe9f8b3230af8a4263e40027a95da883d23 Mon Sep 17 00:00:00 2001 From: Tunde Altfatter Date: Thu, 22 Oct 2020 14:35:30 +0200 Subject: [PATCH] fix(tpd-11668): viewchild not ready issue fixed --- lib/captcha.component.ts | 44 ++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) 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() {