Skip to content Skip to sidebar Skip to footer

Call Typescript Function In Html Without A Click Event

I know how to call a TypeScript when clicking a button, but how do you call a function without that type of event? I would like to call a function when I have an array called chart

Solution 1:

It happens because this chartTest() is incapsulated by Angular and is not accessible outside Angular. But inside angular you can, you just need to:

<div *ngIf="chartData.length">
    {{chartMethod()}}
</div>

But it will add an undefined in html so just test

<div *ngIf="chartData.length">
    {{chartMethod() ? chartMethod() : ""}}
</div>

But I don't think it's html resposibillity call any method, it needs to be done by the component itself in typescript.

Solution 2:

Angular 2, You can call a method automatically by passing a method 'checkFieldAccess()' in [ngClass] In template file write this

 [ngClass]="{check:checkFieldAccess(dataPass)}"

Solution 3:

In your component.ts put his after constructor

ngAfterViewInit(){
    this.getUser();//Put here your function or what you need
  } 

ngAfterViewInit() is called after a component's view, and its children's views, are created. Its a lifecycle hook that is called after a component's view has been fully initialized.

Post a Comment for "Call Typescript Function In Html Without A Click Event"