gpt4 book ai didi

angular - TypeError : control. setParent 不是函数

转载 作者:行者123 更新时间:2023-12-04 11:46:33 27 4
gpt4 key购买 nike

我的组件出现错误,TypeError: control.setParent is not a function .使用 FormBuilder,我构建了一个 FormArray 并在构造函数中启动了表单组。因此,据我所知,这个错误是说尚未设置表单控件。不确定这对我有意义

这是到目前为止的组件......

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { tap, map } from 'rxjs/operators';
import { MediaListService } from '../../services/media-list.service';
import { ActivatedRoute } from '@angular/router';

@Component({
selector: 'app-media-list-builder',
templateUrl: './media-list-builder.component.html'
})
export class MediaListBuilderComponent implements OnInit {

mediaListForm: FormGroup;
contactsDataSource$: Observable<any>;
mediaListDataSource$: Observable<any>;
mediaListId: number;
mediaList: any;
searchText = '';
searchKey = 'name';
tableView = true;

constructor(
private fb: FormBuilder,
private router: ActivatedRoute,
private medialistsrv: MediaListService
) {

this.mediaListForm = this.fb.group({
contacts: this.fb.array([])
});

this.router.params.subscribe(params => {
this.mediaListId = +params['id'];
});
}

get _contacts(): FormArray {
return this.mediaListForm.get('contacts') as FormArray;
}

ngOnInit() {

this.medialistsrv.getMediaListById(this.mediaListId).subscribe(
response => {
this.mediaList = response.contacts;
this.mediaList = this.mediaList.map(obj => obj.contactId);
this.mediaList.forEach(id => {
this._contacts.push(id);
});
}
);

this.contactsDataSource$ = this.medialistsrv.getAllContacts()
.map(response => response);

}

toggleView(arg) {
this.tableView = arg;
}

}

最佳答案

让我们考虑一种对我来说很好的解决方案。

 this.customerFormGroup= this.formBuilder.group({
firstName: ['', Validators.required],
lastName:['', Validators.required],
customerAddressDTOList:this.formBuilder.array([])
});
然后
get customerAddressDTOList() {
return <FormArray>this.customerFormGroup.controls['customerAddressDTOList'];
}
在此执行推送操作之后
addCustomer(){
this.customerAddressDTOList.push(this.formBuilder.control(anyvalue));
}

关于angular - TypeError : control. setParent 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49748018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com