I have a Parent Component where I make a http request, get Observable in return and subscribe to it :
this.bookingService.getBooking().subscribe((r) => {
this.booking = r['result'];
});
I pass the booking variable to Child Component like this:
<child *ngIf="booking" [booking]="booking"></child>
In my child component I receive that variable and I use ngOnChanges to listen to changes on that variable. I need to use this variable immediately as the child component is loaded.
@Input() booking: Booking;
. . .
ngOnChanges(changes: SimpleChanges) {
if (changes['booking']) {
this.createForm(this.booking.reference);
}}
I believe that that before the request is finished in Parent Component the booking variable is passed to child as undefined.
@@@@@@@@@@@@@@@@ EDIT @@@@@@@@@@@@@@@@@
Child component is a modal popup.
In the Parent component I have a button that shows that modal(component) and after clicking that button, ngOnInit
fires up in Child component , however ngOnChanges
does not...
Parent-component.ts:
openUpdateReservationPopup() { this.modalRef = this.modalService.show(UpdateReservationComponent, { class: 'modal-dialog-centered' }); }
Parent-component.html:
<button class="btn btn-primary btn-sm" (click)="openUpdateReservationPopup()" *ngIf="showButton()"> <span class="booking-content-actions-label">Open popup</span> </button>
@@@@@@@@@@@@@ END OF EDIT @@@@@@@@@@@@@@@@
Nothing works so far. I have tried separating in the Parent Component the Observable and passing it to the child component (with async pipe and without it), and then subscribing ( inside ngOnInit() ) to that observable in the Child Component.
Parent-component.ts
this.bookingObservable$ = this.bookingService.getBooking();
this.bookingObservable$.subscribe((r) => {
this.booking = r['result'];
});
Parent-component.html
<child *ngIf="booking" [bookingObservable$]="bookingObservable$ | async"></child>
as well as:
<child *ngIf="booking" [bookingObservable]="bookingObservable$ "></child>
Child-component.ts
@Input() bookingObservable$: any;
ngOnInit() {
this.bookingObservable$.subscribe((val) => {
console.log(val);
this.createReferenceNumberForm(val.reference);
});
}