我有两个模型/实体:Company
和 Founder
具有多对多关系,公司作为后端的拥有实体。全栈应用程序是使用 Jhipster 6.10.5 开发的。 Angular 用于前端。我想和它的创始人一起创建一个反应式的新公司。
这两个模型以 angular 定义,如下所示 - 首先显示 Company
,然后是 Founder
模型:用于创建新公司的代码也如下所示。
我收到两种类型的错误。
<块引用>“对于以下代码片段中显示的以下代码,对象可能是“未定义的”。
创始人名字:公司.创始人.创始人名字,
-类型“IFounder[]”上不存在属性“ FounderFirstName”
founderFirstName: company.founders.founderFirstName,
如何从公司模型定义或访问创始人模型/实体中的元素?
两个模型如下所示: 1-Company.model.ts
import { ITeam } from 'app/shared/model/companyprofile/team.model';
import { IFounder } from 'app/shared/model/companyprofile/founder.model';
export interface ICompany {
id?: number;
companyName?: string;
description?: any;
teams?: ITeam[];
founders?: IFounder[];
}
export class Company implements ICompany {
constructor(
public id?: number,
public companyName?: string,
public description?: any,
public teams?: ITeam[],
public founders?: IFounder[]
) {}
}
2-Founder.model.ts
import { ICompany } from 'app/shared/model/companyprofile/company.model';
export interface IFounder {
id?: number;
founderFirstName?: string;
founderLastName?: string;
companies?: ICompany[];
}
export class Founder implements IFounder {
constructor(public id?: number, public founderFirstName?: string, public founderLastName?: string, public companies?: ICompany[]) {}
}
然后在 Company-update.component.ts 中,这里是创建/更新公司及其创始人的类,我遇到了错误。
export class CompanyUpdateComponent implements OnInit {
isSaving = false;
founders: IFounder[] = [];
teams: ITeam[] = [];
editForm = this.fb.group({
id: [],
companyName: [null, [Validators.required]],
description: [null, [Validators.required]],
founders: this.fb.group({
founderFirstName: [null, [Validators.required]],
founderLastName: [null, [Validators.required]],
}),
});
constructor(
protected dataUtils: JhiDataUtils,
protected eventManager: JhiEventManager,
protected companyService: CompanyService,
protected founderService: FounderService,
protected teamService: TeamService,
protected activatedRoute: ActivatedRoute,
private fb: FormBuilder
) {}
ngOnInit(): void {
this.activatedRoute.data.subscribe(({ company }) => {
this.updateForm(company);
this.founderService.query().subscribe((res: HttpResponse<IFounder[]>) => (this.founders = res.body || []));
this.teamService.query().subscribe((res: HttpResponse<ITeam[]>) => (this.teams = res.body || []));
});
}
updateForm(company: ICompany): void {
this.editForm.patchValue({
id: company.id,
companyName: company.companyName,
description: company.description,
founders: {
founderFirstName: company.founders.founderFirstName,
founderLastName: company.founders.founderLastName,
},
});
}
save(): void {
this.isSaving = true;
const company = this.createFromForm();
if (company.id !== undefined) {
this.subscribeToSaveResponse(this.companyService.update(company));
} else {
this.subscribeToSaveResponse(this.companyService.create(company));
}
}
private createFromForm(): ICompany {
return {
...new Company(),
id: this.editForm.get(['id'])!.value,
companyName: this.editForm.get(['companyName'])!.value,
description: this.editForm.get(['description'])!.value,
...new Founder(),
founderFirstName: this.editForm.get(['founderFirstName'])!.value,
founderLastName: this.editForm.get(['founderLastName'])!.value,
};
}
感谢您花时间帮助我解决这个问题。