-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.component.ts
More file actions
51 lines (45 loc) · 1.4 KB
/
Copy pathcontact.component.ts
File metadata and controls
51 lines (45 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Component } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { RecaptchaComponent, RecaptchaService } from 'recaptcha-angular'
/**
* Example: a contact form that requires a solved reCAPTCHA before submitting.
* The verified token is bound with [(ngModel)]; RecaptchaService tracks state.
*/
@Component({
selector: 'app-contact',
standalone: true,
imports: [RecaptchaComponent, FormsModule],
providers: [RecaptchaService],
template: `
<form (ngSubmit)="submit()">
<textarea [(ngModel)]="message" name="message" required></textarea>
<recaptcha-v2
#captcha
[sitekey]="siteKey"
[(ngModel)]="token"
name="captcha"
(verify)="recaptcha.onVerify($event)"
(expire)="recaptcha.onExpire()"
(error)="recaptcha.onError()"
></recaptcha-v2>
<button type="submit" [disabled]="!recaptcha.isVerified()">Send</button>
</form>
`,
})
export class ContactComponent {
siteKey = 'YOUR_SITE_KEY'
message = ''
token = ''
constructor(public recaptcha: RecaptchaService) {}
async submit(): Promise<void> {
const res = await fetch('/api/contact', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: this.message, 'g-recaptcha-response': this.token }),
})
if (res.ok) {
this.message = ''
this.recaptcha.reset()
}
}
}