I am very new to Angular10 and have been told to do a task.
Task Description:- I have to create a form that gets auto-submitted when all the fields are filled.
But I am unable to do it, I am having difficulty on how I shall use the Resiter(regForm)
function without any button clicks.
the app.component.html file
{{title}}
<div class="container text-center">
<div class="row">
<div class="form-bg">
<form #regForm="ngForm" (ngSubmit)="Register(regForm)">
<h2 class="text-center">Registration Form</h2>
<br>
<div class="form-group">
<input type="text" class="form-control" placeholder="first Name" name="firstName" required ngModel>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="last Name" name="lastName" required ngModel>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="email" name="email" required ngModel>
</div>
<br>
<div class="align-center">
<button type="submit" class="btn btn-primary" id="register">Register</button>
</div>
<ng-template *ngIf="regForm.valid ; then thenBlock">
</ng-template>
<ng-template #thenBlock>
form is now valid
<div (mousemove)="Register(regForm)"></div>
<!-- {{Register(regForm)}} -->
</ng-template>
</form>
</div>
</div>
</div>
app.component.ts file is
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'TemplateDriven';
Register(form : any){
console.log(' button clicked ')
console.log(form.controls.firstName.value);
console.log(form.controls.lastName.value);
}
}
How should I use the Register
function?
One more point, when I am using *ngIf, the function Register
is getting called many number of times.