Angular5: Need to prevent ngbTooltip from rendering before data is returned from observable
Look into using OnInit lifecycle hook which is processed first when a component is loaded.
Documentation For ngOnInit
Thats simple enough, then move that call to the service as seen below into the life cycle hook.
Give the class a public variable ie
public toolTipData: string;
(moved into OnInit)
this.planService.getAuditDetails(planId, fieldName, fieldValue)
.subscribe((auditDetail: AuditDetail) => {
this.tooTipData = auditDetail.toolTipInfo // (guessing at name)
this.auditDetail = auditDetail;
this.loadCompleted = true);
},
(errors) =>
{
console.log(errors) // api error status code.
}
});
In the HTML set the tool tip value to
[ngbTooltip]="toolTipData"
This should help populate the tool tip appropriately.
The asynchronous changes aren't showing up because NgbTooltip window uses ChangeDetectionStrategy.OnPush
and the only way to trigger change detection is calling open()
on a closed tooltip.
In order to access the NgbTooltip API:
Add a template reference to NgbTooltip
<i #tooltip [ngbTooltip]="tipContent" (mouseenter)="refreshAuditDetail()" class="fas fa-info-circle fa-sm"></i>
Bring the reference into the component
@ViewChild('tooltip') tooltip: NgbTooltip;
Hacky Solution
Close and re-open tooltip once the data is loaded. This could cause issues if the user already moved off of the tooltip by the time the data is loaded.
refreshAuditDetail() {
...
this.planService.getAuditDetails(planId, fieldName, fieldValue)
.subscribe((auditDetail: AuditDetail) => {
this.auditDetail = auditDetail;
this.tooltip.close();
this.tooltip.open();
}, () => {}, () => this.loadCompleted = true);
}
}
Better Solution
Set triggers="manual". Call this.tooltip.open()
in refreshAuditDetails
.Add a (mouseleave)
event which cancels the subscription from refreshAuditDetails
and calls this.tooltip.close()
.