Ran into a few problems lately and wonder why I can't access an object, that was set in the ngOnInit function for later purposes, like in another function?
I want to access and use this.appointmentDetailId
in my cancelAppointment()
function but it is undefined
. Hope someone can help. Thanks.
Here's my code:
export class AppointmentDetailComponent implements OnInit {
id: any;
appointmentDetailId: any;
appointmentDetail$: Observable<AppointmentDetails>;
appointmentDetail: AppointmentDetails;
pageTitle = 'Some Default Title Maybe';
constructor(
private route: ActivatedRoute,
private title: Title,
private apiService: APIService
) {
this.appointmentDetailId = this.id;
console.log(this.appointmentDetailId);
}
ngOnInit() {
this.route.paramMap
.pipe(
tap((params: ParamMap) => {
this.id = params.get('id');
// Or this.id = +params.get('id'); to coerce to type number maybe
this.pageTitle = 'Termin Details: ' + this.id;
this.title.setTitle(this.pageTitle);
}),
switchMap(() => this.apiService.getAppointmentDetailsById(this.id))
)
.subscribe((data: AppointmentDetails) => {
this.appointmentDetail = data;
console.log(this.appointmentDetail);
});
}
cancelAppointment() {
console.log(this.appointmentDetailId);
this.apiService.cancelUserAppointment(this.appointmentDetailId);
}
}