Skip to content

Instantly share code, notes, and snippets.

@FazioNico
Last active October 28, 2019 13:23
Show Gist options
  • Save FazioNico/181b0750930f2d582feb17fd702ce27a to your computer and use it in GitHub Desktop.
Save FazioNico/181b0750930f2d582feb17fd702ce27a to your computer and use it in GitHub Desktop.
Simple & Basic Angular Reactive form example
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