Last active
October 28, 2019 13:23
-
-
Save FazioNico/181b0750930f2d582feb17fd702ce27a to your computer and use it in GitHub Desktop.
Simple & Basic Angular Reactive form example
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
import { Component, OnInit } from '@angular/core'; | |
import { Validators, FormGroup, FormControl } from '@angular/forms'; | |
@Component({ | |
selector: 'my-component', | |
template: ` | |
<form [formGroup]="form"> | |
<input formControlName="email" name="email" type="email" /> <br/> | |
<input formControlName="pwd" name="pwd" type="password" /> <br/> | |
<button (click)="submit()">Submit</button> | |
</form> | |
`, | |
style: [``] | |
}) | |
export class MyComponent implements OnInit { | |
public form: FormGroup; | |
constructor() {} | |
ngOnInit(): void { | |
this.form = new FormGroup({ | |
email: new FormControl('', Validators.compose([])), | |
pwd: new FormControl('', Validators.compose([])) | |
}); | |
} | |
submit(): void { | |
if (this.form.valid) { | |
console.log('form value: ', this.form.value); | |
} else { | |
console.log('Error: Form invalid'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment