Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 - infinite loop when i call method from a Angular 2 class inside template

Tags:

angular

I have a problem when i call a method from component in the template by interpolation: {{get_method()}}. The method runs, but in infinite loop I don't know why. Any help please ??

the code of method is like this :

get_name() {
  console.log("bonjour");
}

and I call it in my template like this :

{{get_name()}}

and this is the result :

enter image description here

like image 201
Sofiene Ben Khemis Avatar asked Mar 28 '17 23:03

Sofiene Ben Khemis


2 Answers

You should not use methods in your template, because each time Angular runs change detection, the method will be called, which can happen often. So actually this is not an infinite loop, the method just gets called on each change detection.

To avoid this, you need to change your code to handle the logic of methods in your component, and use variables in your template instead.

like image 199
AT82 Avatar answered Nov 20 '22 09:11

AT82


i finally found the sollution , it's to change Detection Strategy to OnPush for more information visite this link Change Detection Strategy: OnPush

like image 2
Sofiene Ben Khemis Avatar answered Nov 20 '22 10:11

Sofiene Ben Khemis