I am currently working on a component that dynamically renders several components using the following template:
<div [saJquiAccordion]="{active: group.value['collapsed']}" *ngFor="let group of filterGroupsTemplate | keysCheckDisplay;">
<div>
<h4>{{group.key | i18n}}</h4>
<form id="ibo-{{group.key}}" class="form-horizontal" autocomplete="off" style="overflow: initial">
<fieldset *ngFor="let field of group.value | keys">
<ng-container *ngComponentOutlet="fieldSets[field.value.template];
ngModuleFactory: smartadminFormsModule;"></ng-container>
</fieldset>
</form>
</div>
</div>
The challenge I'm facing is that the data required to populate these components is fetched from an API call:
this.getFiltersSubscription = this.getFilters().subscribe(
(filters) => {
this.filters = filters;
log.info('API CALL. getting filters');
// Sending data to fieldform components
this.iboService.updateIBOsRankList(filters['iboRank'].data);
this.iboService.updateIBOsNewsletterOptions(filters['iboNewsletter'].data);
this.iboService.updateIBOsTotalOrders(filters['iboTotalOrders'].data);
}
);
After obtaining the data, I activate a service Observable that my components subscribe to for processing the collected data.
ISSUE
If the API call happens before all components are loaded, the service methods will be triggered with no subscribers.
A potential solution would involve loading the data first, rendering the template only when the data is loaded, and then triggering the service methods (Observables).
I aim to avoid making multiple API calls for each component, considering there could be around 60 components to load. Instead, I prefer a more streamlined approach like this:
// Listens for field initialization and triggers the creation of the fieldset by initiating a service call that will be listened to by the field component
this.iboService.initIBOsFilters$.subscribe(
(fieldName) => {
if (fieldName === 'IBOsRankSelectorFieldComponent') {
log.data('inside initIBOsFilters$ subscription, calling updateIBOsFilters()', fieldName);
this.iboService.updateIBOsRankList(this.filters['iboRank'].data); // HERE I'M PASSING DATA TO THE DYNAMICALLY RENDERED COMPONENT. HOWEVER, IF this.filters IS UNDEFINED, IT WILL CAUSE AN ISSUE
}
}
);
To achieve this, it is crucial to ensure that this.filters
is defined. This leads me to the question:
How can I delay rendering my template HTML until after the API call is completed and this.filters
is defined?
I apologize for the lengthy explanation, but please feel free to ask for more details if needed.
Thank you!