如果相关表单组无效,Angular 7 和表单数组会禁用表单组的按钮,并给出未定义的错误

标签 angular angular7 angular-reactive-forms formarray

我有一个表单数组,为带有按钮的每一行生成表单控件。

如果表单数组中的相关表单组无效,我需要禁用相关按钮。

按钮是:

<button mat-raised-button color="warn" type="submit" color="warn" (click)="addDist(element, i)">
                <mat-icon>add</mat-icon> Add
</button>

采用数组形式:

this.arrayGroup = new FormGroup({
  distribution: new FormArray(this.dataSource.data.map(x => new FormGroup({
    actual_date: new FormControl('', Validators.required),
    note: new FormControl(''),
    kit: new FormControl('', Validators.required)
  })
  ))
});

因此,在每一行中,我都需要禁用它,直到日期和工具包填满为止。如果未填写表格,其他按钮应保持无效。

我尝试过:

<button mat-raised-button color="warn" [disabled]="!arrayGroup.get('distribution')).at(i).valid" type="submit" color="warn" (click)="addDist(element, i)">
                <mat-icon>add</mat-icon> Add
</button>

但出现错误:

Uncaught (in promise): Error: Template parse errors: Parser Error: Unexpected token ')' at column 32 in [!arrayGroup.get('distribution')).at(i).valid]

然后我尝试了:

[disabled]="!arrayGroup.distribution.at(i).valid"

我得到了:

ERROR TypeError: Cannot read property 'at' of undefined at Object.eval [as updateDirectives]

最佳答案

这是一个语法错误。您的代码中的括号过多。

这是更新后的代码:

[disabled]="!arrayGroup.get('distribution').at(i).valid"

关于如果相关表单组无效,Angular 7 和表单数组会禁用表单组的按钮,并给出未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55256534/

相关文章:

javascript - 如何在运行时确定可注入(inject)的 Angular 服务?

angular - 我正在尝试使用 Angular 上传图像但出现错误

Angular 7 - 在应用程序的基本 URL 和 Angular 脚本之间删除斜线

生产版本 : "Property controls does not exist on type AbstractControl" 上的 Angular4 编译错误

angular - Angular2中的路由变量

javascript - 如何在 Angular 通用的服务器上捕获组件错误?

angular - 如何在两个组件之间使用 Angular7( Angular Material )拖放

angular - 错误 TS2554 : Expected 2 arguments, 但使用 @ViewChild 得到 1

Angular react 形式 : sync multiple inputs with a single formControlName

angular - 异步表单验证后自动调用方法 Angular 8