简体   繁体   English

如何在 angular 9 中禁用表单数组的控制项之一

[英]how to disable one of control item of form array in angular 9

I have a form and in this form in this form I have a formArray .我有一个表格,在这个表格中我有一个formArray

  InitialFrom(): void {
this.addElectricMoneyFG = this.fromBuilder.group({
  hasAccountNumber: [this.showMoreInfo],
  moreAccountInfo: ['', null],
  description: [''],
  locales: this.fromBuilder.array([]),
  published: [false]
})

the formArray : formArray

selectedLanguage(langId): FormGroup {
return this.fromBuilder.group({
  languageId: [langId],
  name: [''],
  moreAccountInfo: ['']
})

} }

When clicking on the toggle I want, moreAccountInfo in the formArray to be disabled .单击我想要的切换时, moreAccountInfo中的formArray将被disabled

I wrote this code:我写了这段代码:

this.f.hasAccountNumber.valueChanges.subscribe(check => {
  this.showMoreInfo = check;
  if (check) {
     this.f.locales['controls'][0]['controls']['moreAccountInfo'].enabled;
  } else {
    this.f.locales['controls'][0]['controls']['moreAccountInfo'].enabled;
  }
  this.f.locales.updateValueAndValidity();
  this.cdRef.detectChanges()
})

It didn't work, threw this error:它没有用,抛出这个错误:

this.f.locales.controls[0].controls.moreAccountInfo.disabled is not a function

You need follow this method.你需要遵循这个方法。

 (<FormArray>this.testForm.get("locales")).controls[0].get('moreAccountInfo').disable()

If you want to disable whole form of index 0.如果要禁用索引 0 的整个形式。

(<FormArray>this.testForm.get("locales")).controls[0].disable()

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM