BullyWiiPlaza
BullyWiiPlaza

Reputation: 19195

Clickable image with Angular 2.x

In HTML I want to make a clickable image which then calls a TypeScript function using an Angular directive. I'm using Angular version 2.4.10.

I tried various snippets but none worked and I always get the warning Attribute ng-click is not allowed here:

<a ng-click="myTypeScriptFunction()"> <img src="url-to-my-image"/></a>

With onClick() and an alert() dialog it works fine:

<a onClick="alert('It worked');"> <img src="url-to-my-image"/></a>

How can it be done with Angular?

Upvotes: 4

Views: 26653

Answers (3)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657308

In Angular 2/4 for event binding you use

<a (click)="myTypeScriptFunction()"> ... </a>

This is documented here: https://angular.io/docs

Upvotes: 6

Aravind
Aravind

Reputation: 41571

You should be using click event binding

<button (click)="yourMethod()"> <img src="url-to-my-image"/></button>

Upvotes: 2

omer cohen
omer cohen

Reputation: 282

change for this code :

<a (click)="myTypeScriptFunction()"> <img src="url-to-my-image"/></a>

Upvotes: 4

Related Questions