32
loading...
This website collects cookies to deliver better user experience
FormsModule
inside app.module.ts
file of angular. By importing this module we can use and can enable feature of it inside our component.import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ FormsModule ],
})
export class AppModule{}
<form>
element to track aggregated form value and validation status. As soon as you import FormsModule, this directive becomes active by default on all <form>
tags.< form #heroForm="ngForm" >
<!-- All forms will be generated within this area -->
< /form >
FormControl
instance from the domain model and binds it to a form control element. The ngmodel
directive binds the value of HTML controls (input, select, textarea) to application data.< input type="text" #firstName="ngModel">
<!-- So whatever we write inside input box, angular bind it inside fristName model of component.
< input type="text" #firstName="ngModel" [(ngModel)]="nameModel" name="firstName" >
<form #userRegistrationForm="ngForm">
< input required type="text" [(ngModel)]="userForm.firstName" name="firstName" >
</form>
{ firstName : '' }
<form #heroForm="ngForm" class="container" (ngSubmit)="onSubmitValidator(heroForm.value)">
<div id="parent" *ngFor="let i of [0,1,2]" >
<app-child [id]="i+1" [name]="i+1" ></app-child>
</div>
<hr>
{{heroForm.value | json}}
<hr>
<div>
<app-button-submit></app-button-submit>
</div>
</form>
app-child
component is our custom component and is placed inside ngFor loop. Now lets see whats inside app-child component.viewProviders: [{ provide: ControlContainer, useExisting: NgForm }]
<div class="form-group">
<label [for]="Name">Name</label>
<input required #templatInputRef="ngModel" type="text" class="form-control"
[id]="inputId" [name]="inputName" [(ngModel)]="vname" (keyup)="onChange(templatInputRef)">
{{vname}}
<div class="alert alert-danger" [hidden]="templatInputRef.valid || templatInputRef.pristine">
Name is required
</div>
</div>
<hr>
@Component({
selector: 'app-button-submit',
templateUrl: './button-submit.component.html',
styleUrls: ['./button-submit.component.css'],
providers : [{provide : ControlContainer, useExisting : NgForm}]
})
export class ButtonSubmitComponent {
constructor(private control : NgForm) { }
}
<button type="submit" class="btn btn-success"
[disabled]="!control.form.valid">Submit
</button>