Reputation: 882
In Angular 2 template driven forms, I can write
<form #form="ngForm">
[...]
</form>
ngForm is a FormGroup, as fare as I could understand. How do I grab that object in the corresponding component?
class FormComponent {
formGroup: FormGroup; // How do I have the form INJECTED AUTOMATICALLY by the framework? Something like a ViewChild, but it's not a view
}
It must be simple but the documentation only shows how to use the form in the template.
Thanks
EDIT: the type of what I want to grab is NgForm, not FormGroup!
Upvotes: 1
Views: 5634
Reputation: 1189
You can access any template variable from the component using ViewChild decorator. In you case:
class FormComponent {
@ViewChild('form') formGroup;
ngOnInit() {
this.formGroup.statusChanges().subscribe(() => {
// Your code here!
});
console.log(this.formGroup); // Inspect the object for other available property and methods.
}
}
Upvotes: 2