如何获取嵌套的FormControl?
这是我的表单结构:
form = new FormGroup({
name: new FormControl('', Validators.required),
comment: new FormControl(''),
endpointsPermissions: new FormControl({
read: new FormControl(null),
write: new FormControl(null)
}),
exportDefinitionsPermissions: new FormControl({
read: new FormControl(null),
write: new FormControl(null)
}),
sourcesPermissions: new FormControl({
read: new FormControl(null),
write: new FormControl(null)
})
});
我尝试在我的 html 文件中使用它,如下所示:
<mat-form-field appearance="fill" fxFlex="100">
<mat-checkbox [formControl]="form.get('endpointsPermissions').value.read">{{"ROLES.READ" | translate}}</mat-checkbox>
</mat-form-field>
但它不起作用,我得到的错误是:
control.registerOnChange 不是一个函数
mat-form-field 必须包含 MatFormFieldControl。
那么我如何正确使用[formControl]和嵌套的FormControls。
This is my form structure:
form = new FormGroup({
name: new FormControl('', Validators.required),
comment: new FormControl(''),
endpointsPermissions: new FormControl({
read: new FormControl(null),
write: new FormControl(null)
}),
exportDefinitionsPermissions: new FormControl({
read: new FormControl(null),
write: new FormControl(null)
}),
sourcesPermissions: new FormControl({
read: new FormControl(null),
write: new FormControl(null)
})
});
and I tried to use it in my html-file like this:
<mat-form-field appearance="fill" fxFlex="100">
<mat-checkbox [formControl]="form.get('endpointsPermissions').value.read">{{"ROLES.READ" | translate}}</mat-checkbox>
</mat-form-field>
but its not working and the errors I get are:
control.registerOnChange is not a function
mat-form-field must contain a MatFormFieldControl.
So how do i use [formControl] with nested FormControls correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mat-checkbox 在 mat-form-field 中不起作用。请参阅此内容以查看哪个元素在 mat-form-field 中起作用。 https://material.angular.io/components/form-field/overview
嵌套形式的正确方法是
和
mat-checkbox does not work inside mat-form-field. Refer to this to see which element works inside mat-form-field. https://material.angular.io/components/form-field/overview
Proper way for nested form is
and
现在可以了(感谢 Eliseo):
This works now (thanks to Eliseo):