Angular 为什么在向嵌套表单添加Formarray时会得到数组

Angular 为什么在向嵌套表单添加Formarray时会得到数组,angular,reactive-forms,Angular,Reactive Forms,我正在尝试开发一种与angular2教程非常相似的反应形式。它工作正常,但是当我想添加新问题时,我得到以下错误: 异常:./RecommendationDetailsComponent类中出现错误 建议详细信息组件-内联模板:45:40原因: 找不到路径为“问题->1->id”的控件 删除一个问题很好。有人知道我在哪里可以找到解决方案吗。我不清楚到底是什么问题 模型: export class Recommendation { _id?: string; recommendation:

我正在尝试开发一种与angular2教程非常相似的反应形式。它工作正常,但是当我想添加新问题时,我得到以下错误:

异常:./RecommendationDetailsComponent类中出现错误 建议详细信息组件-内联模板:45:40原因: 找不到路径为“问题->1->id”的控件

删除一个问题很好。有人知道我在哪里可以找到解决方案吗。我不清楚到底是什么问题

模型:

export class Recommendation {
  _id?: string;
  recommendation: string;
  questions: Question[];
  }
}

export class Question {
  id: '';
  question: '';
  date: '';
}
建议-details.component.ts

import { Component, Input, OnChanges } from '@angular/core';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';

import { Recommendation, Question } from '../recommendation';
import { RecommendationService } from '../recommendation.service';

@Component({
  selector: 'recommendation-details',
  templateUrl: './recommendation-details.component.html',
  styleUrls: ['./recommendation-details.component.css'],
  providers: [RecommendationService]
})

export class RecommendationDetailsComponent implements OnChanges {
  @Input()
  Recommendation: Recommendation;

  @Input()
  createHandler: Function;
  @Input()
  updateHandler: Function;
  @Input()
  deleteHandler: Function;

  recommendationForm: FormGroup;

  constructor (private fb: FormBuilder, private recommendationService: RecommendationService) {this.createForm()}

  // creatForm Creert een basisformulier om gegevens te wijzigen
  createForm() {
    this.recommendationForm = this.fb.group({
      recommendation: '',
      topic: '',
      secTopic: '',
      questions: this.fb.array([]),
    })
    console.log('Form created')
  }

  // Elke keer dat een andere Recommendation wordt gekozen, verander dan de waarden van het formulier
  ngOnChanges() {
    if (this.Recommendation == null) {
      console.log('waiting...');
    } else {
      this.recommendationForm.reset ({
        recommendation: this.Recommendation.recommendation
      })
      this.setQuestions(this.Recommendation.questions);
    }
  };

  get questions(): FormArray {
    return this.recommendationForm.get('questions') as FormArray;
      console.log(this.questions);
  }

  setQuestions(questions: Question[]) {
    console.log ('Hallo ' + questions[0].question);
    const questionFGs = questions.map(question => this.fb.group(question));
    const questionFormArray = this.fb.array(questionFGs);
    this.recommendationForm.setControl('questions', questionFormArray);
  }

  addQuestion () {
    console.log(this.questions);
    this.questions.push(this.fb.group(new Question()));
  }

  }
}
HTML


建议:
问题{{i+1}
身份证件:
问题:
日期:
去除
添加
recommendationForm值:{{recommendationForm.value | json}}


仔细查看代码:

export class Question {
  id: '';
  question: '';
  date: '';
}

如果调用
newquestion()
,则此类上没有属性。 Angular对传递给
fb.group
方法的值执行
Object.keys

_reduceControls(controlsConfig: {[k: string]: any}): {[key: string]: AbstractControl} {
  const controls: {[key: string]: AbstractControl} = {};
  Object.keys(controlsConfig).forEach(controlName => {
    controls[controlName] = this._createControl(controlsConfig[controlName]);
  });
  return controls;
}
在您的情况下,
Object.key(新问题())
将是
[]

因此,将其替换为:

export class Question {
  id = '';
  question = '';
  date = '';
}

什么是模板代码?@Roman C这是什么意思?
export class Question {
  id = '';
  question = '';
  date = '';
}