I'm modifying a component (specifically a custom-rendered drop down list) to allow one to pass in a variable in the HTML and pre-select an item from the drop down.
ngAfterViewInit()
seemed like the right place to grab the variable, and then update the view based on it.
I could not get it to work. It did everything except actually update the DOM to reflect the change.
I finally decided that maybe I should just brute-force it and instead of passing variables to functions within the angular I'll just grab the DOM object and click it:
...click()
Again, all the logic would fire, but the state in the DOM wouldn't actually update to reflect that.
I finally tried the "wrap it in a setTimeout" hack:
setTimeout(function(){
...click()
},100);
And...that worked!
Why?
Am I using the wrong hook? Or misunderstanding ngAfterViewInit()
?
I noticed that I could put my call in ngAfterViewChecked()
and it would work just fine there...but the problem, of course, is that hook is being constantly called, so my UI would get stuck in a loop if I place it there.
Is my having to use setTimeout a sign of something wrong elsewhere? (I assume it is, but not sure what that something wrong would be...)