简体   繁体   English

如何访问复杂嵌套 FormGroup 的属性

[英]How can I access a properties of complex nested FormGroup

I am trying to learn reactive forms in Angular.我正在尝试在 Angular 中学习反应式 forms。 I am able to write to all properties BUT numb, alts, letter and text.我能够写入所有属性,但 numb、alts、字母和文本。

**COMPONENT.TS**

createForm() {
    this.userForm = this.fb.group({
      fname: ['', [Validators.required]],
      lname: ['', [Validators.required]],
      email: ['', [Validators.email, Validators.required]],
      facility: ['', [Validators.required]],
      position: [''],
      test: this.fb.group({
        name: [''],
        id: [''],
        date: [''],
        scored: [''],
        wrong: [''],
        duration: [''],
        answers: this.fb.array ([
          this.fb.group({
            numb: [''],
            alts: this.fb.group({
              letter: this.fb.array([]),
              text: this.fb.array([])
            })
          })
        ])
      })
    })
  }

  get answerArray() {
    return this.userForm.get("answers") as FormArray;
  }
**COMPONENT.HTML**

  <form [formGroup]="userForm">
    <div formGroupName="test">
      <div formArrayName="answers">
        <div *ngFor="let ans of answerArray.controls; let i = index">
          <div [formGroupName]="i">
              <input type="text" formControlName="">
          </div>
        </div>
      </div>
    </div>
  </form>
  <pre>{{ userForm.value | json}}</pre>

I get this error: core.js:6241 ERROR TypeError: Cannot read property 'controls' of null我收到此错误: core.js:6241 ERROR TypeError: Cannot read property 'controls' of null

Please if anyone can help me.请如果有人可以帮助我。 I found this example but I was not able to understand https://stackblitz.com/edit/angular-nested-reactive-form-eg-na1ctu?file=src%2Fapp%2Fapp.component.html我找到了这个例子,但我无法理解https://stackblitz.com/edit/angular-nested-reactive-form-eg-na1ctu?file=src%2Fapp%2Fapp.component.html

Try this one:试试这个:

get answerArray() {
   return this.userForm.controls['test'].get("answers") as FormArray;   
}

or或者

get answerArray() {
   return this.userForm.get("test.answers") as FormArray;   
}

or或者

get answerArray() {
   return this.userForm.get(["test","answers"]) as FormArray;   
}

"answers" is inside "test" that's why its returning undefiend. “答案”在“测试”中,这就是为什么它返回 undefiend。

Edit: to Access letter or text you can do the following: you must use an index for this one to know which formGroup in the formArray to get.编辑:要访问字母或文本,您可以执行以下操作:您必须为此使用索引才能知道要获取 formArray 中的哪个 formGroup。

get textArray(index: number): FormArray {      
  return this.answerArray().at(index).get('alts').get('text') as FormArray;
}

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

相关问题 如何访问 FormGroup 中的属性? 我需要访问一些属性(触摸和错误) - How I can access my properties inside the FormGroup? I need to access some properties (touched and errors) 如何在另一个 formGroup 中访问 formGroup 中的 FormArray? - How can i access FormArray inside formGroup inside another formGroup? 如何访问反应式表单组验证? - how can I access to reactive formgroup validations? 我们如何在嵌套表单组的验证器函数中访问最顶层的formControll? - How can we access topmost formControll in a validator function of a nested formgroup? 如何将formGroup添加到另一个formGroup - How can I add a formGroup to another formGroup 如果属性在嵌套的formGroup中,我如何使用formControlName? - How can I use formControlName if the attribute is in a nested formGroup? 如何访问FormArray内的嵌套FormGroup的formControlName - How to access the formControlName of a nested FormGroup inside a FormArray 如何从指令访问 FormGroup - Thinbug - How can I access FormGroup from Directive - Stack Overflow 如何访问 Angular 中嵌套的 object 的属性? - How can I access the properties of a nested object in Angular? 如何订阅 formGroup 更改并从其他两个属性中计算一个? - How can I subscribe to formGroup changes and calculate one of properties from two other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM