영웅 앱으로 Angular 2 튜토리얼을 진행 중입니다. 나는이 튜토리얼의 Http 부분에있다. 나는 아래 heroes.components.ts에서 반환 된 데이터를 사용하기 위해 노력하고있어 hero.service.tsAngular 2 then promise
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data)
.catch(this.handleError);
}
그 다음에 다음과 같은 방법으로 서버에 호출이 있습니다
(link)하지만 나는 그때 제대로 기능을 구현할 수 없습니다. 내가 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();
}
당신은 알고 계십니까이 콘솔 오류는 무엇입니까?