嵌套表单数组出现错误:错误错误:找不到路径为“'问题-> 0->代码'的控件”

Devvvvv

我正在寻求帮助

ERROR Error: Cannot find control with path: 'questions -> 0 -> code'

我不断从此代码进入控制台的错误。我已经咨询过StackOverflow,并进行了一些更改,但错误普遍存在。

我的打字稿文件如下

export class SurveyBuilderPageComponent {
  @Input()
  get survey() {
    return this._survey
  }
  set survey(value: Survey) {
    this._survey = value
    const survey: Survey = value || {
      id: null,
      title: '',
      subtitle: '',
      questions: [
        {
          code: '',
          category: '',
          title: '',
          text: '',
          type: '',
          date: '',
        },
      ],
    }

    this.form = this.formBuilder.group({
      title: [survey.title, Validators.required],
      subtitle: [survey.subtitle, Validators.required],
      questions: this.formBuilder.array(
        survey.questions.map((question) =>
          this.formBuilder.group({
            code: [question.code],
            category: [question.category, Validators.required],
            title: [question.title],
            text: [question.text, Validators.required],
            type: [question.type],
            date: [question.date],
          })
        )
      ),
    })
  }
  @Output() save = new EventEmitter()

  _survey: Survey

  constructor(
    private formBuilder: FormBuilder,
    private surveyService: SurveyService,
    private router: Router
  ) {}

  form: FormGroup

  private newQuestionFormGroup(): FormGroup {
    return this.formBuilder.group({
      title: ['', Validators.required],
      text: ['', Validators.required],
      category: ['', Validators.required],
    })
  }

  submit() {
    if (this.form.valid) {
      this.save.emit(
        this.survey.id
          ? { ...this.form.value, id: this.survey.id }
          : this.form.value
      )
    }
  }
  handleAddNewQuestion() {
    const questions = this.form.get('questions') as FormArray
    this.insertQuestion(questions.controls.length)
  }

  private insertQuestion(index: number) {
    const questions = this.form.get('questions') as FormArray
    questions.insert(index, this.newQuestionFormGroup())
  }

  get questions(): AbstractControl[] {
    return (this.form.get('questions') as FormArray).controls
  }

  handleDeleteQuestion(index: number) {
    const questions = this.form.get('questions') as FormArray
    questions.removeAt(index)
  }

  handleInsertAfterQuestion(event: Event, index: number) {
    event.stopPropagation()
    this.insertQuestion(index + 1)
  }

  handleInsertBeforeQuestion(event: Event, index: number) {
    event.stopPropagation()
    this.insertQuestion(index)
  }
}

我的HTML档案

<div class="container" *ngIf="survey">
  <h2>Survey Details</h2>
  <form [formGroup]="form" (ngSubmit)="submit()">
    <div>
      <mat-form-field class="title-field">
        <input matInput type="text" placeholder="Title" formControlName="title" />
      </mat-form-field>
    </div>
    <div>
      <mat-form-field class="title-field">
        <input matInput type="text" placeholder="Subtitle" formControlName="subtitle" />
      </mat-form-field>
    </div>
    <!--create questions section-->
    <h2>Questions</h2>
    <div class="field-space">
      <!--we should pick a spacing class standard-->

      <ng-container formGroupName="questions">
        <ng-container *ngFor="let question of questions; let idx=index">
          <mat-expansion-panel [formGroupName]="idx" [expanded]="true">
            <mat-expansion-panel-header>
              <mat-panel-title>
                <mat-panel-title class="line-height-40px">
                  <mat-icon class="line-height-40px margin-right-half-em">post_add</mat-icon>
                  Question {{idx+1}}
                  <button mat-icon-button (click)="handleInsertBeforeQuestion($event, idx)">
                    <mat-icon>navigate_before</mat-icon>
                  </button>
                  <button mat-icon-button (click)="handleInsertAfterQuestion($event, idx)">
                    <mat-icon>navigate_next</mat-icon>
                  </button>
                  <button mat-icon-button (click)="handleDeleteQuestion(idx)">
                    <mat-icon>delete</mat-icon>
                  </button>
                </mat-panel-title>
              </mat-panel-title>
            </mat-expansion-panel-header>

            <div class="field-space">
              <mat-form-field class="title-field">
                <input matInput placeholder="Question Code" formControlName="code" />
              </mat-form-field>
            </div>

            <div class="field-space">
              <mat-form-field class="title-field">
                <input matInput formControlName="text" placeholder="Question Text" />
              </mat-form-field>
            </div>
            <div class="field-space">
              <mat-select placeholder="Category" class="question-dropdown" formControlName="category">
                <mat-option value="End Users">End Users</mat-option>
                <mat-option value="Engagement">Engagement</mat-option>
                <mat-option value="Foundations">Foundation</mat-option>
                <mat-option value="Impediments">Impediments</mat-option>
                <mat-option value="Product">Product</mat-option>
                <mat-option value="Scaling">Scaling</mat-option>
                <mat-option value="Team">Team</mat-option>
                <mat-option value="Technical">Technical</mat-option>
              </mat-select>
            </div>
          </mat-expansion-panel>
        </ng-container>
      </ng-container>
    </div>
    <div class="field-space">
      <button mat-button color="secondary" (click)="handleAddNewQuestion()">
        New Question
      </button>
      <button mat-raised-button type="submit" [disabled]="!form.valid" color="primary">
        Submit
      </button>
    </div>
  </form>
</div>

回购在以下链接中

https://github.com/jbrockSTL/misc/tree/main/survey-builder-page

欧文·凯尔文(Owen Kelvin)

解决此类错误的最简单方法是跟踪该错误。

Cannot find control with path: 'questions -> 0 -> code'"

问题-formArray
0-> formGroup
代码-> formControl

您的html结构应反映此更改<ng-container formGroupName="questions"><ng-container formArrayName="questions">

尝试添加新问题时遇到的下一个错误是

Cannot find control with path: 'questions -> 1 -> code'"

注意“ 1”

这意味着表单数组正在运行,唯一的问题是表单控件 code

如果您看您的newQuestionFormGroup()code会丢失

 private newQuestionFormGroup(): FormGroup {
    return this.formBuilder.group({
      title: ["", Validators.required],
      text: ["", Validators.required],
      category: ["", Validators.required]
    });
  }

只需添加即可使用code该功能

在Stackblitz上查看此解决方案上面已经解决了结构,代码应该可以正常工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

表单数组:找不到路径为“列表->说明”的控件

来自分类Dev

cUrl提供恒定的http代码0 =>错误6的问题

来自分类Dev

Angular 8和表单数组错误无法找到路径为index的控件

来自分类Dev

从后台代码更改母版页中的控件属性后出现的问题

来自分类Dev

从背后的代码更改母版页中的控件属性后出现的问题

来自分类Dev

如何解决“问题原因:命名空间CODESIGNING,代码0x2”错误?

来自分类Dev

代码应位于用户控件或使用该控件的表单中

来自分类Dev

使用babel转换JS代码后出现``找不到模块错误''(即使文件转换正确也仍然存在问题)

来自分类Dev

Python键错误= 0-在代码中找不到Dict错误

来自分类Dev

动态数字表单控件问题

来自分类Dev

嵌套控件的Silverlight自动高度问题

来自分类Dev

嵌套控件的Silverlight自动高度问题

来自分类Dev

从GridViewRow获取控件时出现问题

来自分类Dev

动态添加控件时出现的问题

来自分类Dev

错误错误:找不到名称为的控件:

来自分类Dev

从C#代码隐藏错误创建ASP.NET控件

来自分类Dev

错误:错误域= HTTPTask代码= 404“找不到页面” UserInfo = 0x608000228720 {NSLocalizedDescription =找不到页面}

来自分类Dev

我的动态数组出现问题-线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)

来自分类Dev

我的分段控件不会从0变为1。我设置控件的方式是否有问题,或者if语句是否有问题?

来自分类Dev

我的代码不断抛出 http500 错误。找不到任何问题

来自分类Dev

出现错误:使用FormGroup创建动态表单时,找不到名称为''的控件

来自分类Dev

无法安装Windows 10 Creator更新:“出了点问题”错误代码0x800703ed

来自分类Dev

在主文件的代码隐藏中找不到ASP控件

来自分类Dev

后台代码找不到XAML中声明的控件

来自分类Dev

从代码隐藏创建用户控件时找不到静态资源

来自分类Dev

在C ++代码中出现编译错误问题

来自分类Dev

如何编写有问题的代码以避免出现错误?

来自分类Dev

验证程序功能未在表单控件上设置错误对象的问题

来自分类Dev

验证程序功能未在表单控件上设置错误对象的问题

Related 相关文章

  1. 1

    表单数组:找不到路径为“列表->说明”的控件

  2. 2

    cUrl提供恒定的http代码0 =>错误6的问题

  3. 3

    Angular 8和表单数组错误无法找到路径为index的控件

  4. 4

    从后台代码更改母版页中的控件属性后出现的问题

  5. 5

    从背后的代码更改母版页中的控件属性后出现的问题

  6. 6

    如何解决“问题原因:命名空间CODESIGNING,代码0x2”错误?

  7. 7

    代码应位于用户控件或使用该控件的表单中

  8. 8

    使用babel转换JS代码后出现``找不到模块错误''(即使文件转换正确也仍然存在问题)

  9. 9

    Python键错误= 0-在代码中找不到Dict错误

  10. 10

    动态数字表单控件问题

  11. 11

    嵌套控件的Silverlight自动高度问题

  12. 12

    嵌套控件的Silverlight自动高度问题

  13. 13

    从GridViewRow获取控件时出现问题

  14. 14

    动态添加控件时出现的问题

  15. 15

    错误错误:找不到名称为的控件:

  16. 16

    从C#代码隐藏错误创建ASP.NET控件

  17. 17

    错误:错误域= HTTPTask代码= 404“找不到页面” UserInfo = 0x608000228720 {NSLocalizedDescription =找不到页面}

  18. 18

    我的动态数组出现问题-线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)

  19. 19

    我的分段控件不会从0变为1。我设置控件的方式是否有问题,或者if语句是否有问题?

  20. 20

    我的代码不断抛出 http500 错误。找不到任何问题

  21. 21

    出现错误:使用FormGroup创建动态表单时,找不到名称为''的控件

  22. 22

    无法安装Windows 10 Creator更新:“出了点问题”错误代码0x800703ed

  23. 23

    在主文件的代码隐藏中找不到ASP控件

  24. 24

    后台代码找不到XAML中声明的控件

  25. 25

    从代码隐藏创建用户控件时找不到静态资源

  26. 26

    在C ++代码中出现编译错误问题

  27. 27

    如何编写有问题的代码以避免出现错误?

  28. 28

    验证程序功能未在表单控件上设置错误对象的问题

  29. 29

    验证程序功能未在表单控件上设置错误对象的问题

热门标签

归档