Currently in the process of developing an Angular-Cli application that involves multiple models with relational data tables. When fetching data from the server, I need to map this data to corresponding model objects.
I've experimented with creating a single model to represent the database view by merging two different models. However, I found this approach cumbersome and somewhat absurd for my application.
export class Employee{
id:number;
fullName:string;
department: Department;
}
export class Department{
deptId:number;
name:string;
}
#within my service
<-- department table | json -->
[
{deptId: 1, name: "IT"},
{deptId: 2, name: "HR"},
{deptId: 3, name: "Finance"}
]
<-- employee table | json -->
[
{id: 1, fullName: "George W Bush", deptId:1},
{id: 2, fullName: "Barack Obama", deptId:2},
{id: 3, fullName: "Donald Trump", deptId:3},
]
<-- data retrieved from view | json -->
[
{id: 1, fullName: "George W Bush", deptId:1, name: "IT"},
{id: 2, fullName: "Barack Obama", deptId:2, name: "HR"},
{id: 3, fullName: "Donald Trump", deptId:3, name: "Finance"},
]
employees: Employee[]
getData():Observable<Employee[]>{
return this.http.get<Employee[]>(this.stringUrl).subscribe(data=>{
employees = data;
});
}
<ul *ngFor="let employee of employees">
<li>employee.fullName}}
<ul>
<li>{{employee.department}}</li>
</ul>
</li>
</ul>
1. George W Bush
a.IT
2. Barack Obama
b.HR
3. Donald Trump
c.Finance