Currently, I am experimenting with the Angular Heroes Tutorial using Typescript. The code snippet below is functioning correctly while testing out the services:
getHeroes() {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}
However, when I make a slight modification to the code as shown below, it stops working:
getHeroes(){
this.heroService.getHeroes().then(function (heroes:Hero[]) {
this.heroes = heroes;
})
}
An error message is thrown stating:
Unhandled Promise rejection: This is null ; Zone: angular ; Task: Promise.then ; Value: TypeError: This is null
this.heroes = heroes;
To provide context, I have declared 'heroes' within the class like so:
heroes: Hero[];