Why the following code works if I use the arrow function but gives error if used with function keyword?
export class WeatherComponent implements OnInit {
isShowResult = false;
result: any;
constructor(public api: ApiService) {}
ngOnInit(): void {}
changeStatus() {
this.api.modifyStatus();
}
getWeather(city: string) {
this.api.getWeatherReport(city).subscribe
(
(data: any) => {
this.isShowResult = true;
this.result = data;
},
(error: any) => {console.log(error);},
() => {console.log('Api call completed');}
);
}
}
But if I replace arrow function with function keyword it gives error as "'this' implicitly has type 'any' because it does not have a type annotation."
getWeather(city: string) {
this.api.getWeatherReport(city).subscribe(
function(data: any) {
this.isShowResult = true;
this.result = data;
},
(error: any) => {console.log(error);},
() => {console.log('Weather api call completed');}
);
}