I came across the Angular 2 tutorial on routing and found a section where they introduce a route /detail/:id
along with an ngOnInit()
method to handle this route and extract the :id
parameter:
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
let id = +params['id'];
this.heroService.getHero(id)
.then(hero => this.hero = hero);
});
}
What confuses me is why they are iterating over route parameters. Since there's only one ID, wouldn't it be possible to retrieve it using this.route.params['id']
?
Furthermore, if there were indeed multiple IDs present, what would be the purpose of looping through them as each new hero would simply replace the previous one?