角反应式形式循环
目标是拥有一个动态组件,以自动为给定的反应形式呈现所需的错误消息。
.component.html
<ng-template #errTpl let-control>
<div *ngIf="control.value?.invalid && (control.value?.dirty || control.value?.touched)">
<div *ngIf="control.value?.errors?.['required']" class="alert alert-danger">
{{ control.key | titlecase }} is required
</div>
</div>
</ng-template>
<div *ngFor="let control of signUpForm.controls | keyvalue">
<div *ngIf="isFormGroup(signUpForm.get(control.key)); else elseBlock">
<!-- How to loop over controls? -->
<div *ngIf="signUpForm.get(control.key)['controls'] !== null">
...
</div>
</div>
<ng-template #elseBlock>
<ng-container [ngTemplateOutlet]="errTpl" [ngTemplateOutletContext]="{ $implicit: control }">
</ng-container>
</ng-template>
</div>
在仅具有formcontrol
s的表单时,可以正常工作,以处理formgroup
s,以下方法添加到组件中并使用在上面的*ngif
中,检查所处理的内容。
.component.ts
isFormGroup(candidate: any) {
return candidate instanceof FormGroup
}
但是,通过formgroup
控件循环,无法按需工作。
访问:
signUpForm.get(control.key)['controls']
最终进入:
TS2531: Object is possibly 'null'.
即使将访问访问包装到有条件的块中时,也会出现相同的错误消息。
<div *ngIf="signUpForm.get(control.key)['controls'] !== null">
...
</div>
如何访问控件,以便能够将它们传递到循环中的模板上。
还是有一种更好的方法来应用标准错误消息而无需硬编码每个错误消息块,而无需在每个控件上循环?
访问子控制器(编辑)
signUpForm.get(control.key)!.controls
投掷
TS2339: Property 'controls' does not exist on type 'AbstractControl'.
signUpForm.get(control.key)!['controls']
的其他方法:
TS7052: Element implicitly has an 'any' type because type 'AbstractControl' has no index signature. Did you mean to call 'get'?
使用.get()
返回null
。
<div *ngFor="let childControl of control.value!.value | keyvalue">
{{childControl.value?.error}}
使用上述工作来获取childControl
,但尝试获取erros
最终以:
TS2571: Object is of type 'unknown'.
The goal is to have a dynamic component to automatically render required error messages for a given reactive form.
.component.html
<ng-template #errTpl let-control>
<div *ngIf="control.value?.invalid && (control.value?.dirty || control.value?.touched)">
<div *ngIf="control.value?.errors?.['required']" class="alert alert-danger">
{{ control.key | titlecase }} is required
</div>
</div>
</ng-template>
<div *ngFor="let control of signUpForm.controls | keyvalue">
<div *ngIf="isFormGroup(signUpForm.get(control.key)); else elseBlock">
<!-- How to loop over controls? -->
<div *ngIf="signUpForm.get(control.key)['controls'] !== null">
...
</div>
</div>
<ng-template #elseBlock>
<ng-container [ngTemplateOutlet]="errTpl" [ngTemplateOutletContext]="{ $implicit: control }">
</ng-container>
</ng-template>
</div>
Works fine when the form only has FormControl
s, to handle FormGroup
s, the following method is added to the component and used in the *ngIf
above, to check what is handled.
.component.ts
isFormGroup(candidate: any) {
return candidate instanceof FormGroup
}
However, looping over the FormGroup
controls, isn't working as desired.
Accessing:
signUpForm.get(control.key)['controls']
Ends up in:
TS2531: Object is possibly 'null'.
Even when wrapping the accessing into a conditional block, the same error message appears.
<div *ngIf="signUpForm.get(control.key)['controls'] !== null">
...
</div>
How should the controls be accessed, to be able to pass them to the template in a loop.
Or is there even a better way to apply standard error messages without hardcoding each error message block, and without having to loop over each control?
Additional ways to access the child controls (EDIT)
signUpForm.get(control.key)!.controls
throws
TS2339: Property 'controls' does not exist on type 'AbstractControl'.
signUpForm.get(control.key)!['controls']
throws:
TS7052: Element implicitly has an 'any' type because type 'AbstractControl' has no index signature. Did you mean to call 'get'?
Using .get()
returns null
again.
<div *ngFor="let childControl of control.value!.value | keyvalue">
{{childControl.value?.error}}
Using the above works to get the childControl
, but trying to get the erros
ends up in:
TS2571: Object is of type 'unknown'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论