7

dashboard.component.ts

myFunction(){
      console.log("test");
  }

dashboard.component.html

<img [src]="team.logo" onclick="myFunction()" alt="img">

Whenever the image is clicked, error appears in console saying "Reference error: myFunction is not defined". I have tried using ng-click aswell but no luck.

2
  • 1
    Angular should be (click)="myFunction()", have you tried that? Commented May 21, 2020 at 14:36
  • ng-click is the old angularJS (aka angular 1) syntax. Commented May 21, 2020 at 14:45

3 Answers 3

12

You need to do the following,

In your dashboard.component.html file,

<img [src]="team.logo" (click)="myFunction()">
Sign up to request clarification or add additional context in comments.

8 Comments

yes that gets the job done!
same answer posted below
@Sajeetharan, Both the answers got posted in or around the same time frame.
A minute difference, for exactly the same answer. (Hence upvotes vs downvotes)
The accepted answer is not a great answer... I'd read the comments on the OP, and current accepted answer and here and expand your answer (edit, add info)
|
6

You need to use event named (click) with angular

<img [src]="team.logo" (click)="myFunction()" alt="img">

3 Comments

yes that works, and why would ng-click not work in this situation?
ng-click is with angularjs (1.x)
ng-click is not an Angular binding. It was in AngularJs which is very different.
1

In Angular , we use (click) function instead of onClick.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.