Skip to content

Instantly share code, notes, and snippets.

@rajaramtt
Created May 17, 2020 11:44
Show Gist options
  • Save rajaramtt/5b6c6242a8cb10c724e66c94bdb5b066 to your computer and use it in GitHub Desktop.
Save rajaramtt/5b6c6242a8cb10c724e66c94bdb5b066 to your computer and use it in GitHub Desktop.
angular forms
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
export class AppComponent {
form = this.formBuilder.group({
firstName: [''],
lastName: [''],
age: [''],
});
controls = {
firstName: this.form.get('firstName'),
lastName: this.form.get('lastName'),
}
constructor(private formBuilder: FormBuilder) {}
}
<form [formGroup]="form">
<div>
<label for="firstName">First Name</label>
<input formControlName="firstName" id="firstName"/>
<span *ngIf="controls.lastName.touched && controls.firstName.hasError('required')" class="errors">
Field is required
</span>
</div>
<div>
<label for="lastName">First Name</label>
<input formControlName="lastName" id="lastName"/>
<span *ngIf="controls.lastName.touched && controls.lastName.hasError('required')" class="errors">
Field is required
</span>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment