Simple Form To Submit Data To Service In Angular 9 With Bootstrap

Introduction

 
This blog will help you to understand how reactive forms are built to pass data from UI to Service Component. 
 

Background

 
In order to understand this blog one should have a basic understanding of Angular 2+ and Bootstrap.
 
Create a new project in Angular 9 using the following commands:
 
ng new
 
Name the project : RegDemo
 
Select "Yes" for Angular routing.
 
Select "Css" for Styling. 
 
Once the RegDemo project is created go to the directory by using the following command.
 
cd RegDemo
 
Now install Bootstrap with the following command: 
 
npm install bootstrap --save
 
When Bootstrap is installed open angular.json file and add bootstrap.min.css file reference under "styles":
  1. "styles": [  
  2.    "src/styles.css",  
  3.    "node_modules/bootstrap/dist/css/bootstrap.min.css"  
  4. ]  
Now we need to create components and service. Use the following commands to create the same. 
 
ng g c header
ng g c footer 
ng g c reg
ng g c home
ng g s data 
 
Note
g stands for generate | c stands for Component | s stands for Service
 
Open app-routing.modules.ts file and add these lines:
  1. import { RegComponent } from './reg/reg.component';  
  2. import { HomeComponent } from './home/home.component';  
  3.   
  4. const routes: Routes = [  
  5.    { path: "", pathMatch: "full", redirectTo: "home" },  
  6.    { path: "home", component: HomeComponent },  
  7.    { path: "reg", component: RegComponent }  
  8. ];  
 In app.component.html replace the existing code with the below code:
  1. <app-header></app-header>  
  2.    <router-outlet></router-outlet>  
  3. <app-footer></app-footer>  
 Let's start with components now.
 
Open home.component.html file and replace with the code below.
  1. <div class="jumbotron" style="background-color: #fff; height: calc(95vh);">  
  2.     <h1>Angular Bootstrap Demo</h1>  
  3.     <p class="lead">  
  4. This demo shows how to integrate Bootstrap 4 with Angular 7  
  5. </p>  
  6.     <a class="btn btn-lg btn-primary" href="" role="button">View tutorial</a>  
  7. </div>  
Open header.component.html file and replace with the code below.
  1. <nav class="navbar navbar-expand-md bg-dark navbar-dark fixed-top">  
  2.     <a class="navbar-brand" href="#">Angular Bootstrap Demo</a>  
  3.     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse"  
  4. aria-expanded="false" aria-label="Toggle navigation">  
  5.         <span class="navbar-toggler-icon"></span>  
  6.     </button>  
  7.     <div class="collapse navbar-collapse" id="navbarCollapse">  
  8.         <ul class="navbar-nav mr-auto">  
  9.             <li class="nav-item">  
  10.                 <a class="nav-link" routerLink="/home">Home</a>  
  11.             </li>  
  12.             <li class="nav-item">  
  13.                 <a class="nav-link" routerLink="/contact-list">Contacts</a>  
  14.             </li>  
  15.             <li class="nav-item">  
  16.                 <a class="nav-link" routerLink="/contact-create">Create</a>  
  17.             </li>  
  18.         </ul>  
  19.     </div>  
  20. </nav>  
Open header.component.css file and replace with the code below.
  1. .nav-item{  
  2.    padding:2px;  
  3.    margin-left: 7px;  
  4. }  
Open footer.component.html file and replace with the code below. 
  1. <footer>  
  2.    <p class="text-xs-center">© Copyright 2019. All rights reserved.</p>  
  3. </footer>  
Open footer.component.css file and replace with the code below.
  1. footer {  
  2.     position: absolute;  
  3.     right: 0;  
  4.     bottom: 0;  
  5.     left: 0;  
  6.     padding: 1 rem;  
  7.     text - align: center;  
  8.  
Now let's start working with Forms. For this import FormsModule and ReactiveFormsModule in app.modules.ts file.
  1. import{FormsModule,ReactiveFormsModule} from '@angular/forms';  
And add  the below modules under imports:
  1. FormsModule,  
  2. ReactiveFormsModule  
Now let's create a function in Service.
 
Open data.service.ts file and replace with the code below.  
  1. public SaveEmployee(empdata) {  
  2.    console.log("Full Name : " + empdata.regFullName);  
  3.    console.log("Email Id : " + empdata.regEmail);  
  4. }  
Open reg.component.html file and replace with the code below. 
  1. <div class="container" style="margin-top: 150px;">  
  2.     <form [formGroup]="frmRegister" (ngSubmit)="SaveEmployee(frmRegister.value)">  
  3.         <div class="panel panel-primary">  
  4.             <div class="panel-heading">  
  5.                 <h3 class="panel-title">Employee Registration</h3>  
  6.             </div>  
  7.             <div class="panel-body">  
  8.                 <div class="form-group">  
  9.                     <label for="fullName">Full Name</label>  
  10.                     <input id="fullName" formControlName="regFullName" type="text" class="form-control" required />  
  11.                 </div>  
  12.                 <div class="form-group">  
  13.                     <label for="email">Email</label>  
  14.                     <input id="email" formControlName="regEmail" type="email" class="form-control" required />  
  15.                 </div>  
  16.             </div>  
  17.             <div class="panel-footer">  
  18.                 <button type="submit" class="btn btn-primary">Save</button>  
  19.             </div>  
  20.         </div>  
  21.     </form>  
  22. </div>  
Open reg.component.ts file and replace with the code below.
  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. import {  
  6.     FormGroup,  
  7.     FormBuilder  
  8. } from '@angular/forms';  
  9. import {  
  10.     DataService  
  11. } from '../data.service';  
  12. @Component({  
  13.     selector: 'app-reg',  
  14.     templateUrl: './reg.component.html',  
  15.     styleUrls: ['./reg.component.css']  
  16. })  
  17. export class RegComponent implements OnInit {  
  18.     frmRegister: FormGroup;  
  19.     constructor(private _fb: FormBuilder, private dataservice: DataService) {}  
  20.     ngOnInit(): void {  
  21.         this.frmRegister = this._fb.group({  
  22.             regFullName: "",  
  23.             regEmail: ""  
  24.         });  
  25.     }  
  26.     SaveEmployee(value) {  
  27.         this.dataservice.SaveEmployee(value);  
  28.     }  
  29. }  
Now build your application by ng build.
 
Run application by ng serve.
 
 

Summary

 
In this article, I explained how to create a simple angular application to send data from UI to service/api.
 
I believe that I have explained each step clearly so  that it can be easily understood by beginners who want to develop a new application.