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

saran

I have the template view like this.

<p [innerHTML]="myfunction()"></p>

and, ts file be like

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

  myfunction(){
    alert(name);
    return '<div>abcd</div>';

  }
}

Its a simple method calling from html to render the html content through innerhtml, here the myfunction() is calling multiple times, and i'm getting multiple alerts, can anyone help me to solve this.

the stackblitz for this is link

thanks in advance

waterplea

You can use pure pipe to only rerun it if inputs to the pipe have changed:

@Pipe({name: 'functionCaller'})
export class FunctionCallerPipe {
    transform(func: any, ...args: any[]): any {
        return func(...args);
    }
}

And use it like that:

<p [innerHTML]="myfunction| functionCaller : name"></p>
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

  myfunction(name: string){
    alert(name);
    return '<div>' + name + '</div>';
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple times method calling from angular template

From Dev

Why RunListener class testRunStarted() method calling multiple times for a single test in JUnit?

From Dev

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

From Dev

Calling an async method multiple times

From Dev

Angular2 ngfor calling method 3times

From Dev

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

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

Calling angular2 route guard multiple times

From Dev

Angular 2 material mat-select calling multiple times

From Dev

How to write single post service in Angular and call it dynamically multiple times?

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

Angular2: onResize method firing multiple times

From Dev

Calling a method inside *ngIf mutliple times angular

From Dev

Avoid calling function multiple times in angular

From Dev

How to use a function call in a case statement without calling the function multiple times

From Dev

how to keep the previous images when calling matplotlib imshow() multiple times in a single cell google colab?

From Dev

Test a single variable multiple times in python in a single statement

From Java

How to solve if `RotatingServerAdvice` is not fetching file for multiple times?

From Dev

How can I query the same column in a kdb table multiple times in a single statement?

From Dev

How to update multiple columns in single update statement in DB2

From Dev

Avoid calling a scalar function multiple times in a MERGE and UPDATE statement

From Dev

How to avoid Observable calling multiple times RxDart

From Dev

how to prevent calling a function multiple times in Jquery

From Dev

Error with if statement and for loop in method append how to solve?

From Java

Why does Spring execute @Cacheable keyGenerator 2 times for a single invocation of @Cacheable annotated method

From Dev

Angular event method getting called multiple times

Related Related

  1. 1

    Multiple times method calling from angular template

  2. 2

    Why RunListener class testRunStarted() method calling multiple times for a single test in JUnit?

  3. 3

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

  4. 4

    Calling an async method multiple times

  5. 5

    Angular2 ngfor calling method 3times

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

    Calling angular2 route guard multiple times

  10. 10

    Angular 2 material mat-select calling multiple times

  11. 11

    How to write single post service in Angular and call it dynamically multiple times?

  12. 12

    ViewPager InstatiateItem method is calling multiple times

  13. 13

    Reflected Method calling constructor multiple times

  14. 14

    Calling same method multiple times and show progress

  15. 15

    Angular2: onResize method firing multiple times

  16. 16

    Calling a method inside *ngIf mutliple times angular

  17. 17

    Avoid calling function multiple times in angular

  18. 18

    How to use a function call in a case statement without calling the function multiple times

  19. 19

    how to keep the previous images when calling matplotlib imshow() multiple times in a single cell google colab?

  20. 20

    Test a single variable multiple times in python in a single statement

  21. 21

    How to solve if `RotatingServerAdvice` is not fetching file for multiple times?

  22. 22

    How can I query the same column in a kdb table multiple times in a single statement?

  23. 23

    How to update multiple columns in single update statement in DB2

  24. 24

    Avoid calling a scalar function multiple times in a MERGE and UPDATE statement

  25. 25

    How to avoid Observable calling multiple times RxDart

  26. 26

    how to prevent calling a function multiple times in Jquery

  27. 27

    Error with if statement and for loop in method append how to solve?

  28. 28

    Why does Spring execute @Cacheable keyGenerator 2 times for a single invocation of @Cacheable annotated method

  29. 29

    Angular event method getting called multiple times

HotTag

Archive