I am learning angular and API service. Currently, I use router to redirect to the group detail page using group id
Here is what I want to achieve:
In the detail page, I would like to display the user names and user cities in the angular page, which requires multiple API calls (maybe need in order) to get data from the group data.
- I need to use
route id/group idto get the group object viagetGroup() - Each group contains multiple users Id
- (to get user names) I need to iterate each user Ids to get
user objectwhich contains user names and city Ids viagetUser() - (to get city names) I need to iterate each city Ids to get
city objectwhich contains city ids and city names viagetCity()
The overview of all the datas:
######### group data
{
"groups": [
{
"id": "group_id1",
"users": ["user_id1", "user_id2"]
},
{
"id": "group_id2",
"users": ["user_id3", "user_id4"]
}
]
}
######### user data
{
"users": [
{
"id": "user_id1",
"city": ["city_id1", "city_id2"]
},
{
"id": "user_id2",
"city": ["city_id3", "city_id4"]
},
{
"id": "user_id3",
"city": ["city_id1", "city_id3"]
},
{
"id": "user_id4",
"city": ["city_id2", "city_id4"]
}
]
}
######### city data
{
"cities": [
{
"id": "city_id1",
"name": "NY"
}
{
"id": "city_id2",
"name": "BOS"
}
{
"id": "city_id3",
"name": "SF"
}
{
"id": "city_id4",
"name": "LA"
}
]
}
For now, I am able to get the group , but I don't how to get the user names and the cities.
## component.ts
export class displayComponent implements OnInit {
group$!: Observable<group>;
constructor(
private router: Router,
private route: ActivatedRoute,
private groupService: GroupService,
private userService: UserService,
private cityService: CityService,
) {}
ngOnInit(): void {
this.group$ = this.route.params.pipe(
switchMap((params) => this.groupService.getGroup(params.id))
);
// how to iterate all **user Id** to get user names via `getUser(**user Id**)`
// how to iterate all **city Id** to get city names via `getCity(**city Id**)`
}
## html
<div *ngIf="group$ | async as group">
<p> {{ group.id }} </p>
<!-- user names placeholder-->
<!-- user city names placeholder-->
</div>