角接触形式集成(独立和信号)
使用独立组件、反应式信号和 HttpClient 将 Angular 17/18+ 应用程序连接到 Lugatu,以实现可靠的联系表单传送,而无需维护 SMTP 服务器。
1. 具有反应信号的独立联系表组件
现代 Angular 更喜欢独立组件和信号而不是重型 NgModule。 此实现提供了一个可访问的联系表单,其中包含直接提交到 Lugatu 的反应式加载、错误和成功状态。
import { Component, signal, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-contact-form',
standalone: true,
imports: [CommonModule, FormsModule],
template: `
<form (ngSubmit)="onSubmit()" class="space-y-4 max-w-md">
<div>
<label for="name" class="block text-sm font-semibold mb-1">Your Name</label>
<input id="name" type="text" [(ngModel)]="name" name="name" required class="w-full border rounded-xl p-2.5" />
</div>
<div>
<label for="email" class="block text-sm font-semibold mb-1">Email Address</label>
<input id="email" type="email" [(ngModel)]="email" name="email" required class="w-full border rounded-xl p-2.5" />
</div>
<div>
<label for="message" class="block text-sm font-semibold mb-1">Message</label>
<textarea id="message" rows="4" [(ngModel)]="message" name="message" required class="w-full border rounded-xl p-2.5"></textarea>
</div>
<!-- Honeypot spam defense -->
<input type="text" [(ngModel)]="gotcha" name="_gotcha" style="display:none !important" tabindex="-1" autocomplete="off" />
<button type="submit" [disabled]="isSubmitting()" class="btn-primary-editorial py-2.5 px-6">
{{ isSubmitting() ? 'Sending...' : 'Send Message' }}
</button>
<p *ngIf="isSuccess()" class="text-sm text-green-700 font-medium">Thank you! Your message has been received.</p>
<p *ngIf="errorMessage()" class="text-sm text-red-600">{{ errorMessage() }}</p>
</form>
`,
})
export class ContactFormComponent {
private http = inject(HttpClient);
name = signal('');
email = signal('');
message = signal('');
gotcha = signal('');
isSubmitting = signal(false);
isSuccess = signal(false);
errorMessage = signal('');
onSubmit() {
if (this.gotcha()) return; // Silent discard for automated bot submissions
this.isSubmitting.set(true);
this.errorMessage.set('');
const payload = {
fields: {
name: this.name(),
email: this.email(),
message: this.message(),
},
honeypot: this.gotcha(),
};
this.http.post('https://www.lugatu.com/api/v1/public/forms/YOUR_PUBLIC_FORM_ID/submissions', payload)
.subscribe({
next: () => {
this.isSubmitting.set(false);
this.isSuccess.set(true);
this.name.set('');
this.email.set('');
this.message.set('');
},
error: () => {
this.isSubmitting.set(false);
this.errorMessage.set('Submission failed. Please try again.');
},
});
}
}
2. SSR 和 Angular 通用服务器到服务器转发
如果您的 Angular 应用程序使用 Angular SSR(Node / Express 引擎)运行,您可以通过服务器端路由处理程序代理查询,以保护您的 Lugatu 服务器到服务器 API 密钥。
// Express / Node backend endpoint in Angular SSR
server.post('/api/contact', async (req, res) => {
const { name, email, message, _gotcha } = req.body;
if (_gotcha) return res.status(200).json({ ok: true });
try {
const response = await fetch(`https://www.lugatu.com/api/v1/forms/${process.env.LUGATU_FORM_ID}/submissions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.LUGATU_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({ fields: { name, email, message } }),
});
res.status(response.status).json({ ok: response.ok });
} catch (error) {
res.status(500).json({ error: 'Failed to deliver message' });
}
});
3. 机器人防御和蜜罐配置
Lugatu 自动检查 _gotcha 字段。 使用 display:none 和 tabindex="-1" 使其对人类访问者不可见。 填充每个 DOM 输入的垃圾邮件机器人会触发静默抑制。
切勿将表单标签用于蜜罐输入。 关闭自动完成功能并从屏幕阅读器可访问的名称中排除该字段。
安全和身份验证不变
安全不变:服务器到服务器 API 密钥 (lug_live_...) 绝不能捆绑到客户端 Angular 资产或environment.ts 文件中。 使用公共端点直接进行客户端提交或通过服务器端点转发。
故障排除和常见摄取错误
公共表单端点允许从任何浏览器进行跨源提交。 如果您直接从 Angular 调用服务器到服务器端点 (POST /api/v1/forms/{id}/submissions),请切换到公共端点 (POST /api/v1/public/forms/{id}/submissions) 或通过后端转发。
常见问题解答
Lugatu 是否支持带有客户端水合作用的 Angular SSR?
是的。 Lugatu 公共表单提交是在用户交互后通过客户端 fetch 或 HttpClient 执行的,这与 Angular SSR 和客户端 Hydration 完全兼容。
准备好创建您的表单端点了吗?
几秒钟内免费开始使用。 无需信用卡。