Written by: Geoffrey Callaghan

Create a Contact Form With Angular

Create a Contact Form With Angular

To create a contact form with Angular and Fabform.io, follow these steps:

  1. Set Up an Angular Project:

    • Make sure you have Angular installed. If not, you can install it using Angular CLI:
      npm install -g @angular/cli
      ng new angular-fabform-example
      cd angular-fabform-example
  2. Create a Component:

    • Generate a new component for your contact form:
      ng generate component contact-form
  3. Install HttpClientModule:

    • Ensure that you have the HttpClientModule installed for making HTTP requests.
      ng add @angular/common/http
  4. Open the Contact Form Component:

    • Open the contact-form.component.ts file in the src/app/contact-form directory.
  5. Update the Component:

    • Update the component to handle form submission.

      // src/app/contact-form/contact-form.component.ts
      import { Component } from '@angular/core';
      import { HttpClient } from '@angular/common/http';
      
      @Component({
        selector: 'app-contact-form',
        templateUrl: './contact-form.component.html',
        styleUrls: ['./contact-form.component.css'],
      })
      export class ContactFormComponent {
        formData = {
          name: '',
          email: '',
          message: '',
        };
      
        constructor(private http: HttpClient) {}
      
        onSubmit() {
          const fabformUrl = 'YOUR_FABFORM_ENDPOINT'; // Replace with your Fabform.io endpoint
      
          this.http.post(fabformUrl, this.formData).subscribe(
            () => {
              console.log('Form submitted successfully!');
              // You can redirect or perform other actions after successful submission
            },
            (error) => {
              console.error('Error submitting the form:', error);
            }
          );
        }
      }

    Make sure to replace 'YOUR_FABFORM_ENDPOINT' with the actual endpoint URL you obtained from Fabform.io.

  6. Create the Form Template:

    • Open the contact-form.component.html file in the src/app/contact-form directory and update it with your form template:

      <!-- src/app/contact-form/contact-form.component.html -->
      <form (ngSubmit)="onSubmit()">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" [(ngModel)]="formData.name" required>
      
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" [(ngModel)]="formData.email" required>
      
        <label for="message">Message:</label>
        <textarea id="message" name="message" [(ngModel)]="formData.message" required></textarea>
      
        <button type="submit">Submit</button>
      </form>

    Note: Ensure that you have the FormsModule imported in your app.module.ts file to use ngModel.

  7. Update AppModule:

    • Open the app.module.ts file in the src/app directory and add the HttpClientModule to the imports array.

      // src/app/app.module.ts
      import { BrowserModule } from '@angular/platform-browser';
      import { NgModule } from '@angular/core';
      import { HttpClientModule } from '@angular/common/http';
      import { FormsModule } from '@angular/forms';
      
      import { AppComponent } from './app.component';
      import { ContactFormComponent } from './contact-form/contact-form.component';
      
      @NgModule({
        declarations: [AppComponent, ContactFormComponent],
        imports: [BrowserModule, HttpClientModule, FormsModule],
        providers: [],
        bootstrap: [AppComponent],
      })
      export class AppModule {}
  8. Include the Contact Form in AppComponent:

    • Open the app.component.html file in the src/app directory and include the contact form component.

      <!-- src/app/app.component.html -->
      <div>
        <app-contact-form></app-contact-form>
      </div>
  9. Run the Application:

    • Save your files and run the Angular application:
      ng serve

    Open your browser and navigate to http://localhost:4200. You should see the contact form, and you can test it by submitting data.

Remember to replace the placeholder text with your actual Fabform.io endpoint URL. This example assumes you have basic knowledge of Angular and working with APIs.