Я делаю Угловой 2 учебник с Hero App. Я нахожусь в Http-части учебника. (link)Угловой 2 затем обещаю
Существует призыв к серверу со следующим методом в hero.service.ts
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data)
.catch(this.handleError);
}
И тогда я пытаюсь использовать полученные данные в heroes.components.ts ниже, но Я не могу правильно реализовать функцию then. Я получаю эту ошибку консольного
EXCEPTION: Error: Uncaught (in promise): TypeError: this is null
export class HeroesComponent implements OnInit {
title = 'Tour of Heroes';
heroes: Hero[];
selectedHero: Hero;
constructor(
private router: Router,
private heroService: HeroService) { }
getHeroes() {
this.heroService.getHeroes().then(function(value) {
//SUCCESS
console.log(value); //this is working
this.heroes = value; //this is not working
console.log("I am inside then SUCCESS")
console.log(title);
console.log(this.heroes);
}, function(error) {
//FAILURE
console.log(error);
})
}
ngOnInit() {
this.getHeroes();
}
Вы знаете, почему я не могу console.log this.heroes и титула?