In This specific part of the Angular2 Tutorial there is a feature that allows users to add new items to an array. Somehow, when the item is added, the ID is automatically incremented but the exact process behind this automation remains a mystery.
I am aware that Arrays.push() method returns the length of the array, but I am uncertain whether this length value is directly inserted into the id variable within the Hero class.
Within the hero.services.ts file, there exists a code snippet responsible for creating a new hero:
create(name: string): Promise<Hero> {
return this.http
.post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers})
.toPromise()
.then(res => res.json().data)
.catch(this.handleError);
}
Furthermore, in heroes.component.ts, there is an 'add' function implemented as follows:
add(name: string): void {
name = name.trim();
if (!name) { return; }
this.heroService.create(name)
.then(hero => {
this.heroes.push(hero);
this.selectedHero = null;
});
}