Angular 2 - 获取对 formGroup 的引用
Angular 2 - get a reference to the formGroup
在Angular 2个模板驱动的表单中,我可以写
<form #form="ngForm">
[...]
</form>
ngForm 是一个 FormGroup,据我所知。如何在相应的组件中抓取该对象?
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
}
一定很简单,但是文档只展示了如何使用模板中的表单。
谢谢
编辑:我要抓取的类型是 NgForm,而不是 FormGroup!
您可以使用 ViewChild 装饰器从组件访问任何模板变量。在你的情况下:
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.
}
}
在Angular 2个模板驱动的表单中,我可以写
<form #form="ngForm">
[...]
</form>
ngForm 是一个 FormGroup,据我所知。如何在相应的组件中抓取该对象?
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
}
一定很简单,但是文档只展示了如何使用模板中的表单。
谢谢
编辑:我要抓取的类型是 NgForm,而不是 FormGroup!
您可以使用 ViewChild 装饰器从组件访问任何模板变量。在你的情况下:
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.
}
}