getting error as service is undefined if it is called from a function outside of OnInit() or a constructor
Any help is appreciated.
the call to the function from scramble() function is giving error if code is uncommented.
Cannot read property 'getTripsByCriteria' of undefined
code is as follows
export class UpcomingTripsComponent implements OnInit{
private gridApi;
title = "Upcoming Trips";
trip_list: {};
trips: UpcomingTripList;
rowData: any;
private gridColumnApi: any;
last_hours: number = 1;
next_hours: number = 4;
msg: string ='';
gridOptions: any;
private headerHeight;
private rowClassRules;
private smallscreen: boolean = true;
private firsttime: boolean = true;
constructor(private upcomingTripsService: UpcomingTripsService, @Inject('BASE_URL') private baseUrl: string) {
if (sessionStorage.getItem("last_hours")) {
this.last_hours = Number(sessionStorage.getItem("last_hours"));
}
if (sessionStorage.getItem("next_hours")) {
this.next_hours = Number(sessionStorage.getItem("next_hours"));
}
this.headerHeight = 70;
this.rowClassRules = {
"interleaved-rows": function (params) {
return (params.node.rowIndex % 2 !== 0);
}
};
this.upcomingTripsService.getTripsByCriteria(this.last_hours, this.next_hours).subscribe(result => { //works here
this.trips = JSON.parse(result.toString());
this.rowData = this.trips.UpcomingTrips;
}, error => console.error(error));
}
ngOnInit() {
alert('in ngOnInit' + this.upcomingTripsService); //works
}
changeAutorefresh(event) {
alert('in checkbox change');
this.scrambleAndRefreshAll();
}
scrambleAndRefreshAll() {
setInterval(this.scramble, 10000);
}
scramble(params: any) {
alert('in scramble' + this.upcomingTripsService); //error
//this.upcomingTripsService.getTripsByCriteria(this.last_hours, this.next_hours).subscribe(result => {
// this.trips = JSON.parse(result.toString());
// this.rowData = this.trips.UpcomingTrips;
//}, error => console.error(error));
var params1 = { force: true };
this.gridApi.refreshCells(params1);
}