Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable image with Angular 2.x

Tags:

html

angular

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?

like image 684
BullyWiiPlaza Avatar asked Oct 31 '25 15:10

BullyWiiPlaza


1 Answers

In Angular 2/4 for event binding you use

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

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

like image 98
Günter Zöchbauer Avatar answered Nov 02 '25 05:11

Günter Zöchbauer