Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  126] [ 7]  / answers: 1 / hits: 59688  / 8 Years ago, wed, february 24, 2016, 12:00:00

I need some jQuery code to run after my Angular component view has initialized to transform number values into stars.



I put the code in the ngAfterViewInit function, but it is not doing what I need it to. I set a break point in the function and I see that most of the HTML of my component has not even loaded by the time ngAfterViewInit function is called. The parts that have not loaded are generated using *ngFor directive.



How can I get my code to run after this directive has generated all of the html on which I need to operate?


More From » jquery

 Answers
61

When does ngAfterViewInit get called?




ngAfterViewInit() should be called after a component's view, and its children's views, are created. I tested this, and found that children's ngAfterViewInit()s are called before the parent's ngAfterViewInit().



When you say most of the HTML of my component has not even loaded, what do you mean by loaded? Is the HTML in the child component's template, or is it being loaded (or generated) by some other means?




How can I get my code to run after this directive has generated all of the html on which I need to operate?




It would help to see the directive code. If the directive somehow generates HTML that is not contained in Angular templates, then you may need to manually trigger an event (e.g., using an output property) to notify the parent when rendering has completed. Or you could use setTimeout() (but that would not be reliable).




I need some jQuery code to run after my Angular component view has initialized to transform number values into stars.




Where are you getting these number values from? If they are dynamically loaded from a server, you could trigger an event when the data arrives, and update the view at that point.



Also, why not write a component that takes a number as an input property and generates stars using NgFor?


[#63171] Tuesday, February 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeanettee

Total Points: 209
Total Questions: 97
Total Answers: 98

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;