patchValue 设置值并禁用该字段

M. *_*ssa 5 angular angular-reactive-forms

我正在推送带有一些值的 FormArray,并且在加载列表后我需要在某些字段中动态设置禁用属性。我的问题是:有没有办法仅使用 patchValue 设置值并设置禁用属性?

我尝试过类似 this.rowArray.controls[index].patchvalue({format: "NUMBER",disabled:true}) 但它似乎不起作用。

  this.rowList.forEach((el, index) => {
              this.rowArray.push(
                this.fb.group(
                  {
                    name: new FormControl(el.name, [
                      Validators.required,
                      Validators.maxLength(30),
                      Validators.pattern(this.BAN_SPECIAL_BUT_UNDERSCORE)
                    ]),
                    source: new FormControl(el.source, Validators.required)
.....
Run Code Online (Sandbox Code Playgroud)

在那之后

    if (this.rowArray.controls[index].get("source").value === "CSV") {
                this.rowArray.controls[index].patchValue({
                  format: "NUMBER",
                  disabled: true
                });
              }
Run Code Online (Sandbox Code Playgroud)

rowList 是我来自后端的矩阵。

wen*_*jun 13

您可以使用该disable()方法动态地将 FormControl 设置为禁用。

正如文档中所述,patchValue()它将不起作用,因为它仅用于设置 FormControl 的值(而不是状态)。

修补 FormGroup 的值。它接受以控件名称作为键的对象,并尽力将值与组中的正确控件相匹配。

this.rowArray.controls[index].get('source').disable();
Run Code Online (Sandbox Code Playgroud)

  • 对我有用,但值字段没有填充,所以我必须在 POST 方法之前重新启用它,然后重新启动我的启用/禁用方法。 (3认同)