I am trying out the Anuglar Heroes Tutorial for Typescript. While trying out the services the following code is working:
getHeroes() {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}
But when I change to code the following it is not working
getHeroes(){
this.heroService.getHeroes().then(function (heroes:Hero[]) {
this.heroes = heroes;
})
}
I am getting the following error:
Unhandled Promise rejection: this is null ; Zone: angular ; Task: Promise.then ; Value: TypeError: this is null
this.heroes = heroes;
I have defined heroes inside the class as
heroes: Hero[];