I am trying to implement [ngInit] in angular 8 code as said in this link ConvertingAngular1To2ngInit
and it almost worked for me but the problem is it execute more than one time and in some case it go in infinite loop or almost infinite and it crash my page entirely.
so i tried to put the function in the html as {{functionName(5)}}
and it executed multiple either even if i am putting it in *ngIf
directive.
i even started new project and just put the function in html at least it executed four time
i searched a lot for this problem and got stuck in angular life cycle hooks but understood nothing.
Asked
Active
Viewed 2,900 times
2

Saurav Pathak
- 796
- 11
- 32

Hassan Ahmed
- 73
- 4
- 10
-
When you call a method directly in the template, it will run every time Angular Change Detection runs. Thats why you will see it called multiple times. The question is, do you need to implement such functionality? What are you trying to achieve, there may be another way – C.OG Oct 23 '19 at 10:18
-
i want to implement a directive like [ngInit] and it happen also with it. executed multiple time – Hassan Ahmed Oct 23 '19 at 10:31
-
you can use some like `*ngIf="variable && yourFunction()"`. So only when variable is true execute the function – Eliseo Oct 23 '19 at 10:53