私はヒーローアプリでAngular 2チュートリアルをやっています。私はチュートリアルの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関数を正しく実装することができません。私はthis.heroesとタイトルをCONSOLE.LOGすることはできませんなぜあなたは知っています。このコンソールエラーに
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();
}
を取得していますか?