google-recaptcha-angular
Version:
Google functionality reCaptcha for Angular
66 lines (51 loc) • 1.77 kB
text/typescript
import {Directive, EventEmitter, Input, OnInit, Output} from '@angular/core';
declare var grecaptcha: any;
export class ReCaptchaRenderDirective implements OnInit {
onCaptchaSubmit: EventEmitter<any>;
key: string;
render: number;
recaptchaId: string;
recaptchaRenderElm: any;
constructor() { }
ngOnInit() {
this.key = null;
this.render = 2500;
this.recaptchaRenderElm = null;
this.onCaptchaSubmit = new EventEmitter();
setTimeout(() => {
const recaptchaStyle = this.propertiesReCaptcha();
const reCaptchaElm = document.getElementById(this.recaptchaId);
const scripts = document.createElement('script');
scripts.src = 'https://www.google.com/recaptcha/api.js';
scripts.async = true;
scripts.defer = true;
const $this = this;
scripts.onload = function () {
setTimeout(function () {
$this.recaptchaRenderElm = grecaptcha.render(reCaptchaElm, recaptchaStyle);
}, 10);
};
document.getElementsByTagName('head')[0].appendChild(scripts);
}, this.render);
}
recaptchaRestar() {
this.recaptchaExpired();
}
propertiesReCaptcha() {
let recaptchaProperties: any = {};
recaptchaProperties['key'] = this.key;
recaptchaProperties['callback'] = (data: string) => this.onCaptchaSubmit.emit(data);
recaptchaProperties['expired-callback'] = () => this.recaptchaExpired();
return recaptchaProperties;
}
recaptchaExpired() {
if (this.recaptchaRenderElm === null) {
return;
}
grecaptcha.reset(this.recaptchaRenderElm);
this.onCaptchaSubmit.emit(null);
}
}