我正试图在我的项目中用垫子手风琴显示常见问题列表.我有一个静态数组,我已将其集成到Form数组中.当我在我的模板中交互Form数组时,我没有得到显示,并且我得到了错误Cannot read properties of null (reading 'value').你知道为什么吗?你有什么解决方案吗?我在Stackblitz的工作:https://stackblitz.com/edit/angular-una717?file=app%2Fexpansion-overview-example.ts,app%2Fexpansion-overview-example.html

我的代码:

// HTML
<form [formGroup]="faqsForm">
  <div formArrayName="faqsFormArray">
    <mat-accordion class="faqs-accordion">
      <mat-expansion-panel
        class="faqs-expansion-panel"
        *ngFor="let faq of faqsForm.get('faqsFormArray').controls; index as i"
        (opened)="panelOpenState = true"
        (closed)="panelOpenState = false"
      >
        <!-- Header -->
        <mat-expansion-panel-header class="faqs-expansion-panel-header">
          <!-- Question -->
          <mat-panel-title class="faqs-panel-title">
            <!-- Section-->
            <mat-panel-description class="faqs-panel-description">
              {{ faq.get('section').value }}
            </mat-panel-description>
            {{ faq.get('question').value }}
          </mat-panel-title>
        </mat-expansion-panel-header>
        <!-- Content -->
        <p class="faqs-panel-content">{{ faq.get('answer').value }}</p>
      </mat-expansion-panel>
    </mat-accordion>
  </div>
</form>
// TS
  // ********** DECLARATIONS **********//

  // Variables for the accordion
  public panelOpenState = false;

  // For the form
  public faqsForm: FormGroup;
  public faqsFormArray: FormArray;

  public displayFaqsItems = [
    {
      key: '1',
      section: 'Verwaltung',
      question: 'Test1',
      answer: 'dsdsddssddssd',
    },
    {
      key: '2',
      section: 'Absatz',
      question: 'Test2',
      answer: 'DSDSSDSDSDSDSDSDSD',
    },
    {
      key: '3',
      section: 'Beschaffung',
      question: 'Test3',
      answer: 'dghsghdsghsdghgsdhsdghsdghsdgsdghsdghsdgds',
    },
    {
      key: '4',
      section: 'Finanzen',
      question: 'Test4',
      answer: 'fdfdfdfdsssdsdfsdfsdf',
    },
  ];

  constructor(
    private formBuilder: FormBuilder) {}

  ngOnInit() {
    // To initialize forms
    this.initForm();

    // Call formRowsData
    this.displayFormElements(this.displayFaqsItems);
  }

  // Creation of the faqs form
  initForm() {
    // General
    this.faqsForm = this.formBuilder.group({
      faqsFormArray: this.formBuilder.array([]),
    });
  }

  /**
   * To add form structure in the faqs
   */
  public formStructure() {
    const formArray = this.faqsForm.get('faqsFormArray') as FormArray;
    const start =
      this.faqsFormArray && this.faqsFormArray !== null
        ? this.faqsFormArray.controls.length
        : 0;
    for (let i = start; i < start + 1; i++) {
      const rowData = {
        key: new FormControl(`new-${i}`),
        section: new FormControl(''),
        question: new FormControl(''),
        answer: new FormControl(''),
      };
      const createRow = this.formBuilder.group(rowData);
      formArray.push(createRow);
    }
    this.faqsFormArray = formArray;
  }

  /**
   * To show formElements
   * @param accordionRows any
   */
  public displayFormElements(accordionRows: any) {
    const formArray = this.faqsForm.get('faqsFormArray') as FormArray;
    const createRow = this.formBuilder.group(accordionRows);
    formArray.push(createRow);
    this.faqsFormArray = formArray;
  }

推荐答案

您创建的窗体数组不正确.尝尝这个.

public displayFormElements(accordionRows: any) {
  const formArray = this.faqsForm.get('faqsFormArray') as FormArray;
  accordionRows.forEach(element => {
    formArray.push(this.formBuilder.group({
      key: [element.key],
      section: [element.section],
      question: [element.question],
      answer: [element.answer]
    }));
  });
}

Typescript相关问答推荐

Angular 垫页分类和垫排序不起作用

如何从具有给定键列表的对象类型的联合中构造类型

有没有办法适当缩小类型?

如何防止TypeScript允许重新分配NTFS?

替代语法/逻辑以避免TS变量被分配之前使用." "

有没有可能使用redux工具包的中间件同时监听状态的变化和操作

如何判断输入是否是TypeScript中的品牌类型?

将对象属性转换为InstanceType或原样的强类型方法

Angular 错误NG0303在Angular 项目中使用app.Component.html中的NGFor

类型脚本满足将布尔类型转换为文字.这正常吗?

记录键的泛型类型

如果数组使用Reaction-Hook-Form更改,则重新呈现表单

在保留类型的同时进行深层键名转换

常量类型参数回退类型

如何获取受类型脚本泛型约束的有效输入参数

判断映射类型内的键值的条件类型

类型脚本-处理值或返回值的函数

仅当类型为联合类型时映射属性类型

使用NextJS中间件重定向,特定页面除外

使用 fp-ts 时如何使用 TypeScript 序列化任务执行?