Himanshu Porwal
Himanshu Porwal

Reputation: 93

How to use TypeScript on a button click

I am trying to use typescript in my application. for the same I am doing an POC and in POC i want to call a function Defined in TypeScript class on button click.

Is it possible to call the function? If yes then how?

So far I have seen examples where the functions are called on Page load only. I need to call a function on certain event.

Thanks.

Upvotes: 9

Views: 63230

Answers (2)

basarat
basarat

Reputation: 276095

I need to call a function on certain event.

If the function is global e.g. TypeScript:

function foo(){
   alert('foo');
}

Just use onclick e.g. html:

<button onclick="foo()">click me!</button>

Remember TypeScript is (almost) JavaScript.

However I recommend using a framework like angular with TypeScript. For maintainability. As an example take a look at the browser quickstart.

Upvotes: 6

sunny rai
sunny rai

Reputation: 615

HTML WILL BE:

<button (click)="myMethod($event)">Click Event</button>

function in .ts file

private myMethod(event:any):void
{
   alert(JSON.stringify(event));
}

Upvotes: -3

Related Questions