我正在尝试构建一个包含表单的角6页面,使用从projects调用中检索到的数据填充who的REST下拉列表。当我在没有订阅的情况下使用硬编码数据测试所有内容时,表单工作得非常完美。但是,当我在订阅范围内添加rest调用并构建表单时,控制台中会出现3-4次FormGroup错误:
错误: formGroup需要一个FormGroup实例。请给我一张。
我不知道为什么表单在没有订阅调用的情况下工作得很好,可能是异步调用的问题?
我看到了this解决方案,但它对我不起作用,只是完全删除了表单
注意事项:我还有其他几个实现这个的页面,但是没有一个页面有这个问题。
component.ts
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
<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>发布于 2019-05-08 09:39:38
找到了一个解决办法,this.buildf2();函数使用来自projectsList中保存的响应的数据,所以我没有将this.buildForm();添加到响应组中,而是添加了这个函数。它使用更新的响应正确地呈现div。
就像这样:
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;
}
}https://stackoverflow.com/questions/56035543
复制相似问题