首页 > 解决方案 > Angular - 子组件的表单无效

问题描述

我正在建立一个表格。有些字段是必需的。

<form #f = "ngForm">
  
 [...]
 <mat-form-field fxFlex = "10">
        <mat-label translate> location.code </mat-label>
        <input  matInput  type="text" required placeholder="" [(ngModel)]="location.code" name="code">
 </mat-form-field>

 [...]
 <inner-form-component [model]="location.innermodel"></inner-form-component>

</form>

<button mat-raised-button color="primary" (click)="save()" [disabled]="f.invalid"> 
       <mat-icon>save</mat-icon>
       {{ 'generic.submit' | translate }} 
</button>

如果某些必填字段未填写,保存按钮将自动禁用。不幸的是,它不适用于添加其他字段的子组件(内部表单组件)继承的字段。

有没有办法让Angular也用它们禁用按钮?

标签: angular

解决方案


看看这篇文章:https ://medium.com/@a.yurich.zuev/angular-nested-template-driven-form-4a3de2042475

您可能在ControlContainer内部组件中缺少 viewProvider


推荐阅读