'Do we need afterViewInit lifecycle hook anymore in angular?
We know that the afterViewInit lifecycle hook gets triggered once when a component finally finishes the initialization of it's view. Thus if any component has to work with it's viewChild, this lifecycle hook get's it's relevance to be invoked inside the component.
But these days we don't have to depend on afterViewInit lifecycle as we can configure the viewchild with setting the "static" property to "true" to tell angular that we want to resolve the query result before change detection (if this was the case).
My question is, if this was the only purpose of ngAfterViewInit to write the logic that depends on component's viewchild, do we really need to use this lifecycle hook anymore if we can work the other way around?
Solution 1:[1]
You can't always use static: true sometimes the visibility of the viewChild or the child component is dynamic means that it does change, for instance viewChild element where the visibility is based on a directive like *ngIf. you can't use static: true in that case
Solution 2:[2]
self.clear() is a method call. You have to pass the method object to the function instead of calling the method (self.clear instead of self.clear()):
glutDisplayFunc(self.clear())glutIdleFunc(self.clear())
glutDisplayFunc(self.clear)
glutIdleFunc(self.clear)
Note that when you do glutDisplayFunc(self.clear()) the clear method is called and the return value of theclear method( None) is passed to the function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fateh Mohamed |
| Solution 2 | Rabbid76 |
