当我运行命令ng serve --prod我有这个问题:
ng:///home/cristopher_ramirez/Development/angularProjects/lazyPagesRouterTest/src/app/service-order/add-service-order/add-service-order.component.html (28,51) 中的错误:类型参数' FormGroup 不可分配给“ServiceOrder”类型的参数。
“FormGroup”类型中缺少属性“cylinderRefsList”。
请注意,这只发生在我使用 --prod 命令时。
这是我的 HTML 代码(add-service-order.component-html):
<div class="container">
<form [formGroup]="serviceOrderForm" novalidate (ngSubmit)="save(serviceOrderForm)">
<div formArrayName="cylinderRefsList">
<div class="card cardCylinder">
<div *ngFor="let cylinder of serviceOrderForm.controls['cylinderRefsList']['controls']; let i=index" class="panel panel-default">
<div class="card-header">
<span>Cilindro {{i + 1}}</span>
<button type="button" class="close" aria-label="Close" *ngIf="serviceOrderForm.controls['cylinderRefsList']['controls'].length > 1"
(click)="removeCylinder(i)">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="card-body" [formGroupName]="i">
<app-cylinder [group]="serviceOrderForm.controls['cylinderRefsList']['controls'][i]"></app-cylinder>
</div>
</div>
</div>
</div>
我正在关注本指南 https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2 所以你可以看到我实际上在表单组中添加了 cylinderRefsList(我的 TS文件与指南几乎相同)唯一的区别是我使用模型而不是界面。
编辑 1:这是我的 TS 文件
export class AddServiceOrderComponent implements OnInit {
public serviceOrderForm: FormGroup;
errMessage: string;
model;
structures: any = [];
structureName = 'Obras';
isStructure = false;
isDate = false;
websafeKey: any;
orderDate: any;
dateTicks = 0;
role: string;
constructor(
private router: Router,
private structureService: StructureService,
private serviceOrderService: ServiceOrderService,
private fb: FormBuilder,
private userService: UserService
) {
this.structureService.getAllStructures().subscribe(data => {
this.structures = data;
console.log(this.structures);
});
this.role = this.userService.getRoleUser();
}
ngOnInit() {
this.serviceOrderForm = this.fb.group({
cylinderRefsList: this.fb.array([])
});
this.addCylinder();
}
initCylinders() {
return this.fb.group({
colado: ['', Validators.required],
ruptura: ['', Validators.required],
edad: ['', Validators.required],
rev: ['', Validators.required],
altura: ['', Validators.required],
area: ['', Validators.required],
cargaKn: ['', Validators.required],
cargaKgf: ['', Validators.required],
resistenciaKgf: ['', Validators.required],
resistenciaMpa: ['', Validators.required],
resistenciaProy: ['', Validators.required],
resistenciaPorce: ['', Validators.required],
ubicacion: ['', Validators.required]
});
}
addCylinder() {
const control =
<FormArray>this.serviceOrderForm.controls['cylinderRefsList'];
const addrCtrl = this.initCylinders();
control.push(addrCtrl);
}
removeCylinder(i: number) {
// remove address from the list
const control =
<FormArray>this.serviceOrderForm.controls['cylinderRefsList'];
control.removeAt(i);
}
save(model: ServiceOrder) {
this.orderDate = new Date(this.model.year, this.model.month - 1,
this.model.day);
this. dateTicks = ((this.orderDate.getTime() * 10000) +
621356076000000000);
this.serviceOrderService.postServiceOrder(this.dateTicks, this.websafeKey,
model['controls']['cylinderRefsList']['value'] )
.subscribe(data => {
if (this.role === 'Admin' || this.role === 'Director') {
this.router.navigate(['/home/service-order']);
} else if (this.role === 'Tecnico') {
this.router.navigate(['/home-tecnician/service-order']);
} else if (this.role === 'Supervisor') {
this.router.navigate(['/home-supervisor/service-order']);
}
});
}
structureInfo(structureName: string, websafeKey: string) {
this.structureName = structureName;
this.websafeKey = websafeKey;
}