Home / คำแนะนำ / การรวมแบบฟอร์มการติดต่อเชิงมุม (สแตนด์อโลนและสัญญาณ)
คู่มือการบูรณาการ · เชิงมุมและสปา

การรวมแบบฟอร์มการติดต่อเชิงมุม (สแตนด์อโลนและสัญญาณ)

เชื่อมต่อแอปพลิเคชัน Angular 17/18+ กับ Lugatu โดยใช้ส่วนประกอบแบบสแตนด์อโลน สัญญาณปฏิกิริยา และ HttpClient เพื่อการส่งแบบฟอร์มการติดต่อที่เชื่อถือได้โดยไม่ต้องบำรุงรักษาเซิร์ฟเวอร์ SMTP

1. ส่วนประกอบแบบฟอร์มการติดต่อแบบสแตนด์อโลนพร้อมสัญญาณปฏิกิริยา

Modern Angular ให้ความสำคัญกับส่วนประกอบและสัญญาณแบบสแตนด์อโลนมากกว่า NgModules ที่มีน้ำหนักมาก การใช้งานนี้ให้แบบฟอร์มการติดต่อที่สามารถเข้าถึงได้พร้อมการโหลดแบบโต้ตอบ ข้อผิดพลาด และสถานะความสำเร็จที่ส่งโดยตรงไปยัง Lugatu

contact-form.component.ts typescript
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 SSR (กลไก Node / Express) คุณสามารถสอบถามพร็อกซีผ่านตัวจัดการเส้นทางฝั่งเซิร์ฟเวอร์เพื่อปกป้องคีย์ Lugatu Server-to-Server API ของคุณ

server.ts typescript
// 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. การป้องกันบอทและการกำหนดค่า Honeypot

Lugatu ตรวจสอบฟิลด์ _gotcha โดยอัตโนมัติ ทำให้ผู้เยี่ยมชมมองไม่เห็นโดยใช้ display:none และ tabindex="-1" สแปมบอทที่เติมทุกอินพุต DOM จะทำให้เกิดการระงับโดยไม่โต้ตอบ

tip

ห้ามใช้ป้ายกำกับแบบฟอร์มสำหรับอินพุต honeypot ปล่อยให้การเติมข้อความอัตโนมัติปิดอยู่ และแยกฟิลด์ออกจากชื่อที่โปรแกรมอ่านหน้าจอสามารถเข้าถึงได้

ความปลอดภัยและการรับรองความถูกต้องไม่แปรผัน

ค่าคงที่ด้านความปลอดภัย: คีย์ API แบบเซิร์ฟเวอร์ต่อเซิร์ฟเวอร์ (lug_live_...) จะต้องไม่ถูกรวมเข้ากับทรัพย์สินเชิงมุมฝั่งไคลเอ็นต์หรือไฟล์ Environmental.ts ใช้จุดสิ้นสุดสาธารณะสำหรับการส่งไคลเอ็นต์โดยตรงหรือส่งต่อผ่านจุดสิ้นสุดของเซิร์ฟเวอร์

การแก้ไขปัญหาและข้อผิดพลาดในการนำเข้าทั่วไป

HTTP 403 FORBIDDEN_BROWSER_ORIGIN เมื่อส่งจาก Angular

จุดสิ้นสุดของแบบฟอร์มสาธารณะอนุญาตให้ส่งข้ามต้นทางจากเบราว์เซอร์ใดก็ได้ หากคุณกำลังเรียกตำแหน่งข้อมูลแบบเซิร์ฟเวอร์ถึงเซิร์ฟเวอร์ (POST /api/v1/forms/{id}/submissions) โดยตรงจาก Angular ให้สลับไปยังตำแหน่งข้อมูลสาธารณะ (POST /api/v1/public/forms/{id}/submissions) หรือส่งต่อผ่านแบ็กเอนด์

คำถามที่พบบ่อย

Lugatu รองรับ Angular SSR ด้วยการให้ความชุ่มชื้นแก่ลูกค้าหรือไม่

ใช่. การส่งแบบฟอร์มสาธารณะของ Lugatu จะดำเนินการผ่านการดึงข้อมูลฝั่งไคลเอ็นต์หรือ HttpClient หลังจากการโต้ตอบของผู้ใช้ ซึ่งเข้ากันได้อย่างสมบูรณ์กับ Angular SSR และไฮเดรชั่นของไคลเอ็นต์

พร้อมที่จะสร้างจุดสิ้นสุดของแบบฟอร์มแล้วหรือยัง?

เริ่มต้นฟรีในไม่กี่วินาที ไม่ต้องใช้บัตรเครดิต

เริ่มฟรี →