Setting Up Angular Project
Run the below command to generate a brand new Angular 9 project using Angular CLI.
Create a simple form in Angular using Bootstrap form component, open the app.component.html file and replace existing code with the following code.
Now we will add the template-driven form using ngModel directive in Angular component. Add the following code in the app.component.html file to initialize the Angular 9 template-driven form.
Run the below command to generate a brand new Angular 9 project using Angular CLI.
ng new angular-template-driven-formHead over to the Angular form project.
cd angular-template-driven-formInstall Bootstrap UI framework via NPM using following command.
Install Bootstrap in Angular 9
npm install bootstrapAdd the Bootstrap CSS path in styles array inside the angular.json file.
"styles": [Now we have configured Angular and Bootstrap, run the command to open the app in the browser.
"src/styles.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
]
ng serve --openTo get started with Form control and NgModel Angular Forms service, we require to import FormsModule in app.module.ts file.
Importing FormsModule
import { BrowserModule } from '@angular/platform-browser';Create Form with Bootstrap From Template
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Create a simple form in Angular using Bootstrap form component, open the app.component.html file and replace existing code with the following code.
<form novalidate>Also, add the following options array for select drop-down input field in the app.component.ts file.
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control">
</div>
<div class="form-group">
<label>Hobbies</label>
<select class="form-control">
<option value="">Select an option</option>
<option *ngFor="let hoby of Hobbies" [value]="hoby">
{{hoby}}
</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-danger btn-block">Submit</button>
</div>
</form>
import { Component } from '@angular/core';Implement Template-driven Form in Angular
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
Hobbies: string[] = [
'Acrobatics',
'Acting',
'Animation',
'Astronomy',
'Baking'
];
}
Now we will add the template-driven form using ngModel directive in Angular component. Add the following code in the app.component.html file to initialize the Angular 9 template-driven form.
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" [(ngModel)]="model.name" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" [(ngModel)]="model.email" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" [(ngModel)]="model.password" required>
</div>
<div class="form-group">
<label>Hobbies</label>
<select class="form-control" name="hobbies" [(ngModel)]="hobbies">
<option value="">Select an option</option>
<option *ngFor="let hoby of Hobbies" [value]="hoby">
{{hoby}}
</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-danger btn-block">Submit</button>
</div>
</form>