How to use TypeScript on button click

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

Can a function be called? If so, how?

So far, I've seen examples where functions are only called on page load. I need to call a function for a specific event.

Thank.

+3


source to share


2 answers


I need to call a function on a specific event.

If the function is global, eg. TypeScript:

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

      

Just use onclick

eg. html:



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

      

Remember TypeScript (almost) JavaScript.

However, I recommend using a framework like angular with TypeScript. For maintainability. As an example, consider: http://youtu.be/Km0DpfX5ZxM

+3


source


HTML WILL:

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

      



in the .ts file

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

      

0


source







All Articles