【发布时间】:2019-12-14 06:08:39
【问题描述】:
首先感谢@AJT_82 的兜售建议!
我一直在研究一个更简单的代码示例来重现错误,可以在 stackblitz.com/edit/angular-42gobh 找到
对有问题的行进行了注释,以便您检查正确的结果应该是什么。 只需取消注释 <div [formGroup]="i"></div> 让一切崩溃。
基本上,我有一个服务为我的组件构建表单,而 HTML 文件使用 Angular Material。当手风琴用于 formArray 时,应用程序完全崩溃并且无法正确分配 formGroup:
customer-edit.service.ts:
import { Injectable } from '@angular/core';
import { FormGroup, FormBuilder, FormArray, Validators } from '@angular/forms';
@Injectable({
providedIn: 'root'
})
export class CustomerEditService {
private cusForm: FormGroup = this.fb.group({
thirdParty: this.fb.group({
name: this.fb.control(null),
vat: this.fb.control(null),
corpoPhone: this.fb.control(null),
corpoMail: this.fb.control(null),
corpoWeb: this.fb.control(null),
activityNumber: this.fb.control(null),
addresses: this.fb.array([]),
contacts: this.fb.array([])
}),
docRefs: this.fb.group({}),
commentsArr: this.fb.group({})
});
constructor(
private fb: FormBuilder
) { }
// **** EMPTY FORMS GETTERS ****
getAddressForm(address?: any) {
const addressForm: FormGroup = this.fb.group({
street: this.fb.control(null),
streetcomp: this.fb.control(null),
streetcomp2: this.fb.control(null),
city: this.fb.control(null),
cp: this.fb.control(null),
state: this.fb.control(null),
country: this.fb.control(null),
main: this.fb.control(null)
});
if (address) {
addressForm.setValue(address);
}
return addressForm;
}
getFilledThirdPartyForm(thirdParty?: any) {
const thirdPartyForm: FormGroup = this.fb.group({
name: this.fb.control(null, Validators.required),
vat: this.fb.control(null, Validators.required),
corpoPhone: this.fb.control(null, Validators.required),
corpoMail: this.fb.control(null, Validators.required),
corpoWeb: this.fb.control(null, Validators.required),
activityNumber: this.fb.control(null),
});
if (thirdParty) {
Object.keys(thirdParty).map(
el => {
if (Object.keys(thirdPartyForm.controls).indexOf(el) !== -1 && el !== 'addresses') {
thirdPartyForm.get(el).setValue(thirdParty[el]);
}
});
}
return thirdPartyForm;
}
}
这是构建表单的组件的 TS 文件。该表单是基于“现实生活”中的 JSON 对象(第三方)构建的,该对象通过 HTTP 请求来自数据库:
import { Component, OnInit, Input } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormArray } from '@angular/forms';
import { CustomerEditService } from '../customer-edit.service';
@Component({
selector: 'app-basic-edit',
templateUrl: './test.component.html'
})
export class testComponent implements OnInit {
thirdParty: any =
{
"addresses": [
{
"street": "AVENIDA ESTADOS UNIDOS, 141",
"streetcomp": "",
"streetcomp2": "",
"city": "SAN BARTOLOME DE TIRAJANA ",
"cp": "35290",
"state": "PALMAS (LAS)",
"country": "spain",
"main": true
},
{
"street": "OTRA DIRECCION DUMMY",
"streetcomp": "",
"streetcomp2": "",
"city": "MADRID",
"cp": "280007",
"state": "MADRID",
"country": "spain",
"main": false
}
],
"contacts": [
{
"_id": "5cf0f6f2a3e9cf847c5861af",
"title": "Mrs.",
"role": "CFO",
"firstName": "John",
"lastName": "Doe",
"phone": "912345654",
"mobile": "673369900",
"thirdParty_id": "5cf0f6d0a3e9cf847c5861aa",
"addresses": [
{
"street": "AVENIDA ESTADOS UNIDOS , 141",
"streetcomp1": "TUNTE",
"streetcomp2": "",
"cp": "35290",
"city": "SAN BARTOLOME DE TIRAJANA ",
"state": "PALMAS (LAS)"
}
],
"email": "jdoe@ketchup.com",
"auditTrail": {
"creation": {
"user_id": "1",
"creationDate": "1559213796974"
},
"modification": [
{
"user_id": "1",
"modifDate": "1559213833358"
}
]
}
}
]
};
thirdPartyForm: FormGroup;
constructor(
private fb: FormBuilder,
private cusEditService: CustomerEditService
) {
}
ngOnInit() {
this.thirdPartyForm = this.cusEditService.getFilledThirdPartyForm(this.thirdParty);
const addresses: any[] = this.thirdParty.addresses;
const addressesFormArr: FormArray = new FormArray([]);
addresses.forEach(
address => {
const currAddressForm: FormGroup = this.cusEditService.getAddressForm(address);
addressesFormArr.push(currAddressForm);
});
this.thirdPartyForm.setControl(
'addresses',
addressesFormArr
);
console.log(this.thirdPartyForm.get('addresses'));
}
onSubmit() {
console.log('Submitted');
}
}
这是 HTML:
<h1>Addresses Test</h1>
<form [formGroup]="thirdPartyForm" (ngSubmit)="onSubmit()">
<div formArrayName="addresses">
<mat-accordion>
<mat-expansion-panel *ngFor="let address of thirdPartyForm.get('addresses').value; index as i">
<mat-expansion-panel-header>
{{ address.city }}
</mat-expansion-panel-header>
<div [formGroupName]="i"> <!-- UNCOMMENTING THIS LINE MAKES EVERYTHING CRASH -->
</div>
</mat-expansion-panel>
</mat-accordion>
</div>
</form>
【问题讨论】:
-
由于 http 请求,stackblitz 不起作用。还有很多代码。您应该隔离您面临的具体问题,并通过创建minimal reproducible example 来表明这一点。太好了,您提供了一个堆栈闪电战,但如果它没有显示您遇到的问题,那它几乎没用;)
-
正如我所担心的那样,尝试在更简单的示例上重现问题并不会重现错误...:stackblitz.com/edit/angular-42gobh
-
好的,那还不错。现在逐个添加更多代码,直到您可以重现相同的错误。我很乐意看一下,其他人可能也是……但是代码太多了,我认为没有人会再看一眼,甚至找出这些代码从哪里来-ps 在您最初提出的堆栈闪电战中,太多了。不要刻意刻薄。
-
会的。感谢您的跟进。当我崩溃时,我会告诉你的! ;)
-
完成! :P ...添加角度材料(扩展面板)就像在我的应用程序中一样破坏了一切...(显然)我留下了
<div [formGroupName]="i">评论(使代码中断的部分),以便您了解它应该如何工作将其注释掉以使浏览器崩溃...
标签: angular angular-material ngfor formarray formgroups