项目作者: JamesHenry

项目描述 :
:sparkles: Easily use Google's reCAPTCHA within your Angular forms
高级语言: TypeScript
项目地址: git://github.com/JamesHenry/angular-google-recaptcha.git
创建时间: 2017-11-15T21:08:37Z
项目社区:https://github.com/JamesHenry/angular-google-recaptcha

开源协议:MIT License

下载


Angular Google reCAPTCHA


Travis
GitHub license
NPM Version
NPM Downloads
Commitizen friendly
semantic-release



typescriptcourses.com



Google’s reCAPTCHA is an awesome, UX-friendly way of ensuring that the users who are submitting your forms are actually humans.

Angular has fantastic built in forms functionality which makes it easy to write powerful custom components and validation logic.

This library makes it effortless to combine them!

If you ever need to reference the full reCAPTCHA docs you can find them here: reCAPTCHA docs

Installation

Head over to the command line and use your favourite package manager to install and save it as a dependency:

E.g.

  1. npm install --save angular-google-recaptcha

or

  1. yarn add angular-google-recaptcha

Usage

1. If you haven’t yet registered your site for use with reCAPTCHA, you’ll need to do that next.

Head over to:

https://www.google.com/recaptcha/admin#list

…and register your site.

Once you have done that, a “site key” will have been generated for you. You need to find this and copy it to you clipboard as it will be important in the next step!

2. Now all you need to do is pass that site key into the library, and this is done via the forRoot convention on the NgModule which the library exposes.

E.g.

  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { ReactiveFormsModule } from '@angular/forms';
  4. import { RecaptchaModule } from 'angular-google-recaptcha';
  5. @NgModule({
  6. imports: [
  7. BrowserModule,
  8. ReactiveFormsModule,
  9. RecaptchaModule.forRoot({
  10. siteKey: 'YOUR_SITE_KEY_HERE',
  11. }),
  12. ],
  13. bootstrap: [AppComponent]
  14. })
  15. export class AppModule { }

As you might expect, you need to be using the @angular/forms package (either the FormsModule or ReactiveFormsModule) within your project, otherwise this library will have nothing to hook into.

3. With everything wired up, you can now use the <recaptcha> component!

E.g. For reactive forms

  1. import { Component } from '@angular/core';
  2. import { FormControl } from '@angular/forms';
  3. @Component({
  4. selector: 'app',
  5. template: `
  6. <recaptcha
  7. [formControl]="myRecaptcha"
  8. (scriptLoad)="onScriptLoad()"
  9. (scriptError)="onScriptError()"
  10. ></recaptcha>
  11. `
  12. })
  13. export class AppComponent {
  14. myRecaptcha = new FormControl(false);
  15. onScriptLoad() {
  16. console.log('Google reCAPTCHA loaded and is ready for use!')
  17. }
  18. onScriptError() {
  19. console.log('Something went long when loading the Google reCAPTCHA')
  20. }
  21. }

E.g. For template-driven forms

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app',
  4. template: `
  5. <recaptcha
  6. [(ngModel)]="myRecaptcha"
  7. (scriptLoad)="onScriptLoad()"
  8. (scriptError)="onScriptError()"
  9. ></recaptcha>
  10. `
  11. })
  12. export class AppComponent {
  13. myRecaptcha: boolean
  14. onScriptLoad() {
  15. console.log('Google reCAPTCHA loaded and is ready for use!')
  16. }
  17. onScriptError() {
  18. console.log('Something went long when loading the Google reCAPTCHA')
  19. }
  20. }