It helps to know that the callback functions are not executed where they are declared. Angular manages the Observables outside the context of your component. The callback function in the subscribe function declared there in the ngOnInit will be run by angular every time the observable returned from the getChannelStatsById()
function emits a value. Even after the ngOnInit has long completed.
When the callback function executes, the rowData
value will be set and the HTML template should update automatically. An *ngIf
check can also be used to see if you have values before displaying the data
<ng-container *ngIf="rowData?.length > 0">
{{ rowData }}
</ng-container>
I am assuming the getChannelStatsById()
function makes an HTTP call in which case it returns an observable that will most probably emit only one value on a successful response from the HTTP call. But it is possible to configure http calls to emit multiple values too. The flow of execution in the component creation or the lifecycle hook like the ngOnInit function does not stop for the observable to emit a value.
Also, very important to remember is observables can emit values indefinitely and angular may keep executing those callback functions after the component has been destroyed. Using async pipe in the HTML template would make life easy as angular handles the subscription OR unsubscribing in the ngOnDestroy lifecycle hook.