I want to create a base form component, that will have additional control over all other forms.
For example, I want to check whether the form is dirty, when user wants to close the form, or not.
Here is my base component form sample:
@Component({
selector: 'my-modal-form',
templateUrl: './modal-form.component.html',
styleUrls: ['./modal-form.component.scss']
})
export class ModalFormComponent implements OnInit, AfterContentInit {
@ContentChild(FormGroup, { static: false }) usersForm: FormGroup;
constructor() { }
ngOnInit(): void {
console.log(this.usersForm);
}
ngAfterContentInit() {
console.log(this.usersForm);
}
}
And here is usage example:
<my-modal-form>
<form [formGroup]="profileForm">
<input type="text" formControlName="firstName" />
<input type="text" formControlName="lastName" />
</form>
</my-modal-form>
So both console.log calls gives me undefined. Here I want to get access to FormGroup instance so I can check is it dirty or not.
The question is - how to correctly get access to projected form (FormGroup)?