In This section of Angular2 Tutorial has the function of adding new elements to an array. When adding, the identifier automatically increases, but I can not understand which process does this.
I know that Arrays.push () returns the length of the array, is this length automatically inserted into the id variable in the Hero class?
Hero.services.ts has this code block for creating a 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);
}
There is an addition in the heroes.component.ts file
add(name: string): void {
name = name.trim();
if (!name) { return; }
this.heroService.create(name)
.then(hero => {
this.heroes.push(hero);
this.selectedHero = null;
});
}
source
share