Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function using href in angular 2/ type script

I want to call a function in type script component from <a> tag using href like this:

  <a href="fun()" ></a>

Is it possible in some way? Note that I need only this way. Using a click event does not help in my case.

I tried already this:

<a href="javascript:fun()"> 

But it does not work.

thanks!

like image 784
levi Avatar asked Oct 29 '17 11:10

levi


People also ask

Can we call a function in href?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).

How do you call a typescript function in HTML?

You need to arrange things so the function is in scope and then you can just call it with {{getConfigurations(param)}} but people can't give you proper help without more info.

Can we use href in angular?

AngularJS ng-href DirectiveThe ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code.


2 Answers

you cannot call function in href use role attribute and make <a> as button and call the function with (click)

<a role="button" (click)="func()"></a>
like image 92
Arun Kumaresh Avatar answered Sep 20 '22 11:09

Arun Kumaresh


I know this is an old question. This worked for me on Angular 6 -

<a href="javascript:void(0);" (click)="getSampleCSV()">CSV Template</a>
like image 35
N K Avatar answered Sep 16 '22 11:09

N K