Created
May 17, 2020 11:44
-
-
Save rajaramtt/5b6c6242a8cb10c724e66c94bdb5b066 to your computer and use it in GitHub Desktop.
angular forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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