So I'm trying to use an object in my html page, for this I want to get the object from my api and put it in a variable to later use its data. My API works fine, it returns the Parking object and when I log it it shows the needed data. When I try to assign that object to a variable and log that variable it gives me UNDEFINED.
export class ParkingdetailsComponent implements OnInit {
parking: Parking;
id: number;
constructor(
private _route: ActivatedRoute,
private _pds: ParkingDataService) { }
ngOnInit(): void {
this._route.paramMap.subscribe(params =>{
this.id = parseInt(params.get('id'));
});
this._pds.getParking$(this.id).subscribe((parking: Parking) =>{
console.log(parking); //logs the Parking object
this.parking = parking;
});
console.log(this.parking) //logs UNDEFINED
}
}
Okay so I fixed my issue, it didn't have anything to do with the subscribe part, it did do what I wanted it to do, the issue was in my HTML I called the objects data with {{ parking.name }} ... forgot the "this." should have been {{ this.parking.name }}
Posting it for others who might stumble on the same issue.