Multiple times method calling from angular template

Front End Developer

I am using method inside a template to call the function, that returns boolean value. The issue is that the function is called more than 6 times. After that I used changeDetectionStrategy.onPush which reduced the calls to 2 times. Below is my code

HTML

<div *ngIf="(checkboolObs(check$)) | async"></div>

TS

check$: Observable<service> = this.data.getresponse();

ngOnInit() {
  this.checkboolObs();
}
checkboolObs(style):boolean {
  return somestyleIDS.includes(style.component)
}

If the value is found it returns true, but it is called several times i need to call it only once. The reason behind this is because checkboolobs() is not triggered some times while navigating that's why I am calling this method from the template.

Християн Христов

This is normal as every time the change-detection cycle is called it will evaluate the template and your function is part of it, so the function will be re-evaluated(called again).

A brief explanation of how does the change detection work in Angular

Imagine that each time there is an event, that might cause any change (for example clicks, some time intervals are doing something, ajax calls) Angular takes the template and re-evaluates it for you out of the box (in other words updates the HTML).

When you use the onPush strategy you are basically telling angular to stop listening for any of the mentioned changes because you will be the one in charge of telling the framework when a change has occurred. ** Disclaimer we have the | async that can be used in templates and it will work fine with the onPush strategy and if your inputs change by reference change detection will be triggered, but as I said this is a brief explanation.

So the thing that you can do is (i assume that you are getting the ids from the check$ observable)

myCheck$ = this.check$.pipe(map(arrayOfIds => arrayOfIds.includes(style.component)))

and then use myCheck$ inside your template with |async.

If this doesn't work please explain in more detail your use case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling an async method multiple times

From

Calling method from a Angular 2 class inside template

From Dev

ViewPager InstatiateItem method is calling multiple times

From Dev

Reflected Method calling constructor multiple times

From Dev

Calling same method multiple times and show progress

From Dev

Calling a method inside *ngIf mutliple times angular

From Dev

Calling a static template method from a template function

From Dev

Avoid calling function multiple times in angular

From Dev

Why Angular 2+ innerHTML is calling method multiple times in a single statement, How to solve this

From Dev

{{ call() }} in template executes the method block multiple times?

From Dev

Is calling computed property from inside Vue method multiple times affecting the performance?

From Dev

Create component template to use it multiple times - Angular

From Dev

Calling a template method from another template method in C++?

From Dev

function called multiple times from template

From Dev

Calling service method from a directive template

From Dev

Angular event method getting called multiple times

From Dev

Angular 4 Subscribe method call multiple times

From Dev

How to stop multiple times method calling of didUpdateLocations() in ios

From Dev

Swift: NavigationLink calling destination's init method multiple times

From Dev

How to use the Scanner in a separate method without calling it multiple times

From Dev

.NET how to prevent calling a method returning a string multiple times

From Dev

Angular subscribe method is calling multiple time

From Dev

Angular2 ngfor calling method 3times

From Dev

Is calling wait() on a std::future multiple times and from multiple threads safe?

From Dev

NSNotificationCenter is calling multiple times

From Dev

Calling virtual method of base template from derived variadic template class

From Dev

Why my subject observable subscriber is calling multiple times in angular?

From Dev

Angular @Input binding using function calling multiple times

From Dev

Calling angular2 route guard multiple times

Related Related

  1. 1

    Calling an async method multiple times

  2. 2

    Calling method from a Angular 2 class inside template

  3. 3

    ViewPager InstatiateItem method is calling multiple times

  4. 4

    Reflected Method calling constructor multiple times

  5. 5

    Calling same method multiple times and show progress

  6. 6

    Calling a method inside *ngIf mutliple times angular

  7. 7

    Calling a static template method from a template function

  8. 8

    Avoid calling function multiple times in angular

  9. 9

    Why Angular 2+ innerHTML is calling method multiple times in a single statement, How to solve this

  10. 10

    {{ call() }} in template executes the method block multiple times?

  11. 11

    Is calling computed property from inside Vue method multiple times affecting the performance?

  12. 12

    Create component template to use it multiple times - Angular

  13. 13

    Calling a template method from another template method in C++?

  14. 14

    function called multiple times from template

  15. 15

    Calling service method from a directive template

  16. 16

    Angular event method getting called multiple times

  17. 17

    Angular 4 Subscribe method call multiple times

  18. 18

    How to stop multiple times method calling of didUpdateLocations() in ios

  19. 19

    Swift: NavigationLink calling destination's init method multiple times

  20. 20

    How to use the Scanner in a separate method without calling it multiple times

  21. 21

    .NET how to prevent calling a method returning a string multiple times

  22. 22

    Angular subscribe method is calling multiple time

  23. 23

    Angular2 ngfor calling method 3times

  24. 24

    Is calling wait() on a std::future multiple times and from multiple threads safe?

  25. 25

    NSNotificationCenter is calling multiple times

  26. 26

    Calling virtual method of base template from derived variadic template class

  27. 27

    Why my subject observable subscriber is calling multiple times in angular?

  28. 28

    Angular @Input binding using function calling multiple times

  29. 29

    Calling angular2 route guard multiple times

HotTag

Archive