如何获取嵌套的FormControl?

发布于 2025-01-11 00:19:31 字数 957 浏览 3 评论 0原文

这是我的表单结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2025-01-18 00:19:31

mat-checkbox 在 mat-form-field 中不起作用。请参阅此内容以查看哪个元素在 mat-form-field 中起作用。 https://material.angular.io/components/form-field/overview

嵌套形式的正确方法是

form = new FormGroup({
        name: new FormControl('', Validators.required),
        comment: new FormControl(''),
        endpointsPermissions: new FormGroup({
            read: new FormControl(null),
            write: new FormControl(null)
        }),
        exportDefinitionsPermissions: new FormGroup({
            read: new FormControl(null),
            write: new FormControl(null)
        }),
        sourcesPermissions: new FormGroup({
            read: new FormControl(null),
            write: new FormControl(null)
        })
    });

<div fxLayout="column" fxFlex formGroupName="endpointsPermissions">
        <h2 mat-dialog-title>Permission</h2>
        <!-- Endpoints -->
        <div fxLayout="row" fxFlex fxLayoutGap="16px">
            <span>Endpoints Permission</span>
            <mat-checkbox matInput formControlName="read">Read</mat-checkbox>
        </div>
    </div>

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

form = new FormGroup({
        name: new FormControl('', Validators.required),
        comment: new FormControl(''),
        endpointsPermissions: new FormGroup({
            read: new FormControl(null),
            write: new FormControl(null)
        }),
        exportDefinitionsPermissions: new FormGroup({
            read: new FormControl(null),
            write: new FormControl(null)
        }),
        sourcesPermissions: new FormGroup({
            read: new FormControl(null),
            write: new FormControl(null)
        })
    });

and

<div fxLayout="column" fxFlex formGroupName="endpointsPermissions">
        <h2 mat-dialog-title>Permission</h2>
        <!-- Endpoints -->
        <div fxLayout="row" fxFlex fxLayoutGap="16px">
            <span>Endpoints Permission</span>
            <mat-checkbox matInput formControlName="read">Read</mat-checkbox>
        </div>
    </div>
羞稚 2025-01-18 00:19:31

现在可以了(感谢 Eliseo):

form = new FormGroup({
    name: new FormControl('', Validators.required),
    comment: new FormControl(''),
    endpointsPermissions: new FormGroup({
        read: new FormControl(null),
        write: new FormControl(null)
    }),
    exportDefinitionsPermissions: new FormGroup({
        read: new FormControl(null),
        write: new FormControl(null)
    }),
    sourcesPermissions: new FormGroup({
        read: new FormControl(null),
        write: new FormControl(null)
    })
});
<mat-checkbox [formControl]="form.get('endpointsPermissions.read')">{{"ROLES.READ" | translate}}</mat-checkbox>

This works now (thanks to Eliseo):

form = new FormGroup({
    name: new FormControl('', Validators.required),
    comment: new FormControl(''),
    endpointsPermissions: new FormGroup({
        read: new FormControl(null),
        write: new FormControl(null)
    }),
    exportDefinitionsPermissions: new FormGroup({
        read: new FormControl(null),
        write: new FormControl(null)
    }),
    sourcesPermissions: new FormGroup({
        read: new FormControl(null),
        write: new FormControl(null)
    })
});
<mat-checkbox [formControl]="form.get('endpointsPermissions.read')">{{"ROLES.READ" | translate}}</mat-checkbox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文