首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >订阅后生成表单会出现"expect formgroup实例“错误

订阅后生成表单会出现"expect formgroup实例“错误
EN

Stack Overflow用户
提问于 2019-05-08 07:22:26
回答 1查看 82关注 0票数 0

我正在尝试构建一个包含表单的角6页面,使用从projects调用中检索到的数据填充who的REST下拉列表。当我在没有订阅的情况下使用硬编码数据测试所有内容时,表单工作得非常完美。但是,当我在订阅范围内添加rest调用并构建表单时,控制台中会出现3-4次FormGroup错误:

错误: formGroup需要一个FormGroup实例。请给我一张。

我不知道为什么表单在没有订阅调用的情况下工作得很好,可能是异步调用的问题?

我看到了this解决方案,但它对我不起作用,只是完全删除了表单

注意事项:我还有其他几个实现这个的页面,但是没有一个页面有这个问题。

component.ts

代码语言:javascript
复制
import { Component, OnInit, ViewChild, ViewContainerRef, AfterContentInit } from '@angular/core';
import { Location } from '@angular/common';
import { TranslatePipe } from 'src/app/pipes/translate/translate.pipe';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DynamicTextFieldService } from 'src/app/services/dynamic-loading/dynamic-text-field/dynamic-text-field.service';
import { DynamicDropdownService } from 'src/app/services/dynamic-loading/dynamic-dropdown/dynamic-dropdown.service';
import { DynamicTextAreaService } from 'src/app/services/dynamic-loading/dynamic-text-area/dynamic-text-area.service';
import { CustomSearchService } from 'src/app/services/search/custom-search/custom-search.service';

@Component({
  selector: 'app-knowledge-base-create',
  templateUrl: './knowledge-base-create.component.html',
  styleUrls: ['./knowledge-base-create.component.scss']
})
export class KnowledgeBaseCreateComponent implements OnInit, AfterContentInit {

  @ViewChild('f1', { read: ViewContainerRef }) f1;
  @ViewChild('f2', { read: ViewContainerRef }) f2;
  @ViewChild('f3', { read: ViewContainerRef }) f3;
  @ViewChild('f4', { read: ViewContainerRef }) f4;

  kbForm: FormGroup;

  projectsList: any;

  constructor(private location: Location,
    private translate: TranslatePipe,
    private dynamictextfield: DynamicTextFieldService,
    private fb: FormBuilder,
    private dynamicdropdown: DynamicDropdownService,
    private dynamictextarea: DynamicTextAreaService,
    private search: CustomSearchService) { }

  ngOnInit() {
    this.search.searchCall("PP_1_Projects", "", "", true).subscribe(
      response => {
        response = response.records;
        this.projectsList = response;
        this.buildForm();
      },
      error => {
        console.log(error);
      }
    );
  }

  buildForm() {
    this.kbForm = this.fb.group({
      'f1': ['', Validators.required],
      'f2': ['', Validators.required],
      'f3': ['', Validators.required],
      'f4': ['', Validators.required]
    })
  }

  ngAfterContentInit() {
    this.buildf1();
    this.buildf2();
    this.buildf3();
    this.buildf4();
  }

  goBack() {
    this.location.back();
  }

  buildf1() {
    let data = {
      reference: this.f1,
      type: "text",
      class: "form-control",
      placeholder: "Article Name", //TODO: change to translate
      id: "f1",
      autoComplete: false,
      formControlName: "f1",
      group: this.kbForm
    }
    this.dynamictextfield.loadTextField(data);
  }

  buildf2() {
    let data = this.projectsList;
    let array = {
      reference: this.f2,
      id: "f2",
      formControlName: "f2",
      group: this.kbForm,
      data: data,
      placeholder: "Select Project Name"
    }
    this.dynamicdropdown.loadDropdown(array);
  }

  buildf3() {
    let data = [
      {
        "name": "New"
      },
      {
        "name": "Open"
      },
      {
        "name": "Closed"
      }
    ]

    let array = {
      reference: this.f3,
      id: "f3",
      formControlName: "f3",
      group: this.kbForm,
      data: data,
      placeholder: "Select Status"
    }
    this.dynamicdropdown.loadDropdown(array);
  }

  buildf4() {
    let data = {
      reference: this.f4,
      placeholder: "Enter any notes here..", //TODO: change to translate
      id: "f4",
      formControlName: "f4",
      group: this.kbForm
    }
    this.dynamictextarea.loadTextArea(data);
  }


}

component.html

代码语言:javascript
复制
<div>
  <app-toolbar></app-toolbar>
  <app-side-nav></app-side-nav>
  <div class="container-fluid">
    <app-button description="{{ 'pages[knowledge_base][buttons][go_back]' | translate }}" class="btn btn-danger btn-md custom" (callFunction)="goBack()"></app-button>
    <div class="card-group">
      <div class="card p-4">
        <form [formGroup]="kbForm">
          <h2 class="col-md-6">Create New Knowledge Base Article</h2>
          <div class="col-md-3">
            <div #f1 class="input"></div>
            <div #f2 class="input"></div>
            <div #f3 class="input"></div>
          </div>
          <div class="col-md-6">
            <div #f4 class="input"></div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
EN

回答 1

Stack Overflow用户

发布于 2019-05-08 09:39:38

找到了一个解决办法,this.buildf2();函数使用来自projectsList中保存的响应的数据,所以我没有将this.buildForm();添加到响应组中,而是添加了这个函数。它使用更新的响应正确地呈现div。

就像这样:

代码语言:javascript
复制
import { Component, OnInit, ViewChild, ViewContainerRef, AfterContentInit } from '@angular/core';
import { Location } from '@angular/common';
import { TranslatePipe } from 'src/app/pipes/translate/translate.pipe';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DynamicTextFieldService } from 'src/app/services/dynamic-loading/dynamic-text-field/dynamic-text-field.service';
import { DynamicDropdownService } from 'src/app/services/dynamic-loading/dynamic-dropdown/dynamic-dropdown.service';
import { DynamicTextAreaService } from 'src/app/services/dynamic-loading/dynamic-text-area/dynamic-text-area.service';
import { CustomSearchService } from 'src/app/services/search/custom-search/custom-search.service';
import { CustomValidators } from 'src/app/views/forgot-password/reset-password/custom-validators';


@Component({
  selector: 'app-knowledge-base-create',
  templateUrl: './knowledge-base-create.component.html',
  styleUrls: ['./knowledge-base-create.component.scss']
})
export class KnowledgeBaseCreateComponent implements OnInit, AfterContentInit {

  @ViewChild('f1', { read: ViewContainerRef }) f1;
  @ViewChild('f2', { read: ViewContainerRef }) f2;
  @ViewChild('f3', { read: ViewContainerRef }) f3;
  @ViewChild('f4', { read: ViewContainerRef }) f4;

  projectsList: any;

  kbForm: FormGroup;

  constructor(private location: Location,
    private translate: TranslatePipe,
    private dynamictextfield: DynamicTextFieldService,
    private fb: FormBuilder,
    private dynamicdropdown: DynamicDropdownService,
    private dynamictextarea: DynamicTextAreaService,
    private search: CustomSearchService) { }

  ngOnInit() {
    this.buildForm(); //removed call from here, replaced with buildForm()
  }

  buildForm() {
    this.kbForm = this.fb.group({
      'f1': ['', Validators.required],
      'f2': ['', Validators.required],
      'f3': ['', Validators.required],
      'f4': ['', Validators.required]
    })
  }

  ngAfterContentInit() {
    this.buildf1();
    this.buildf3();
    this.buildf4();
    this.retrieveProjectData(); //added function here, renders perfectly
  }

  retrieveProjectData() { //created this function instead, added to afterContentInit
    this.search.searchCall("PP_1_Projects", "", "", true).subscribe(
      response => {
        response = response.records;
        this.projectsList = response;
        this.buildf2();
        console.log(response);
      },
      error=>{
        console.log(error);
      }
    )
  }

  goBack() {
    this.location.back();
  }

  buildf1() {
    let data = {
      reference: this.f1,
      type: "text",
      class: "form-control",
      placeholder: "Article Name", //TODO: change to translate
      id: "f1",
      autoComplete: false,
      formControlName: "f1",
      group: this.kbForm
    }
    this.dynamictextfield.loadTextField(data);
  }

  buildf2() {
    let data: any;
    if (this.projectsList == undefined) {
      data = [
        {
          "name": ""
        }
      ]
    } else {
      data = this.projectsList;
    }

    let array = {
      reference: this.f2,
      id: "f2",
      formControlName: "f2",
      group: this.kbForm,
      data: data,
      placeholder: "Select Project Name"
    }
    this.dynamicdropdown.loadDropdown(array);
  }

  buildf3() {
    let data = [
      {
        "name": "New"
      },
      {
        "name": "Published"
      },
      {
        "name": "Unpublished"
      }
    ]

    let array = {
      reference: this.f3,
      id: "f3",
      formControlName: "f3",
      group: this.kbForm,
      data: data,
      placeholder: "Select Status"
    }
    this.dynamicdropdown.loadDropdown(array);
  }

  buildf4() {
    let data = {
      reference: this.f4,
      placeholder: "Enter any notes here..", //TODO: change to translate
      id: "f4",
      formControlName: "f4",
      group: this.kbForm
    }
    this.dynamictextarea.loadTextArea(data);
  }

  get form() {
    return this.kbForm.controls;
  }


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56035543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档