Fellows!
I'm writing a component, for example a table, for my web application. The table component is sometimes hidden, which means that it's in the component tree but not displayed to the user. For example:
<custom-tab> <!-- shown -->
<custom-table />
</custom-tab>
<custom-tab> <!-- NOT shown -->
<custom-table />
</custom-tab>
The hidden (not shown) custom-table component loads it's data inside the ngOnInit-method, which is executed by angular for both tables (shown and not shown). If I add a specific ngIf to the table (which evaluates if the tab is shown), the ngOnInit method is not called. This solution is very cumbersome, as there can be very complex scenarios of a large hierarchies of components which are deciding if a component is shown or not. Looking in the DOM of the browser, I see that correct custom-table elements are not present in the DOM. Angular initialises these components anyway (and calls ngOnInit).
Question:
- How does angular communicate that a component in is basically in the DOM and can't be shown (disregarding CSS)?
- How can one meaningful prohibit components from loading data if they are not shown, and only load data if they are displayed to the user?
Thanks!