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:
Set Up an Angular Project:
npm install -g @angular/cli
ng new angular-fabform-example
cd angular-fabform-example
Create a Component:
ng generate component contact-form
Install HttpClientModule:
ng add @angular/common/http
Open the Contact Form Component:
contact-form.component.ts
file in the src/app/contact-form
directory.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.
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
.
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 {}
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>
Run the 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.