How do I run code in Angular 2+ after a check cycle was done?
Currently we are using setTimeout
, with timing of 1 to achieve this. Isn't there a more readable way to do it?
Our current code looks something like this:
doSomething();
window.setTimeout(() => doSomethingAfterACheckCycle(), 1);
I also know about afterViewChecked, but that is a centralised function for a component, I would need a lot of flags to track cycles. Isn't there an inline way to do it? I only have problem with the notation of setTimeout 1, it makes the code baffling for a junior developer.
We can also create a service call, which internally would use setTimout, I would only like to know if there is already something implemented in Angular for this.
It would also be nice to have linking instead of nesting, something like:
nextCheck(() => soSomething()).nextCheck(() => doSomethingElse());
Edit
We have several scenarios needing this, but an example: We are binding model state to the active/disabled state of tabs. But we also set the selected tab based on url. A problem was changing the model for the tab to become active and also selecting it, did not work on the same change cycle. We had to change the model, which in the next change cycle enabled the tab, then we set is as selected. Another trick here was to manually enable the tab indifferent of the binded (bound?) variable so it would work on the same change cycle.