I have this service in the services file which is an http call.
export class BackendServices {
addons: IAddons[];
constructor(private http: HttpClient) {
this.getWorkOrders();
}
getAddons() {
this.http.get<IAddons>(this.BASE_URL + '/addons/')
.subscribe((data: any) => {
this.addons = data;
});
}
COMPONENT PART Then from my main component I have a button which calls the modal dialog. Here is the code of the main component:
export class OrdersComponent {
openAddonsDialog() {
let dialogRef = this.pickAddonsDialog.open(PickaddonsComponent);
}
}
And then I am calling the getAddons method which is on the service from the constructor of the dialog. I am just wondering how do we make the modal dialog open only when the addons property has received data from the http call.
export class PickaddonsComponent implements OnInit {
constructor(public bs: BackendServices) {
this.bs.getAddons();
}
But it says this.service.getAddons() is undefined. Read a lot about this on stackoverflow, tried quite a few steps from different posts, but none helped me so far.
It seems that initialized BackendService has attribute addons undefined. Which is true. Because it is set only when calling method getAddons.
Try this:
export class BackendServices {
addons: IAddons[] = [];
constructor(private http: HttpClient) {
this.getWorkOrders();
}
getAddons() {
return this.http.get<IAddons>(this.BASE_URL + '/addons/')
.subscribe((data: any) => {
this.addons = data;
});
}
Replacing
addons: IAddons[];
with
addons: IAddons[] = [];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With