Add click event to image in Angular 4

angular 6 image click
angular click event on div
angular 2 dynamic click event
angular 4 click event example
ng-click angular 4
ng-click in angular 7
typescript onclick event type angular
angular trigger click

I need to add a click event to image in angular component. I tried:

<img src="../../assets/add.svg" width="18" height="18" (click)="addItem()"> 

but this does not work. If put image inside button like this:

<button type="button" class="btn_img" (click)="addItem()">
  <img src="../../assets/add.svg" width="18" height="18"> 
 </button>

It looks like a button with an image inside, I do not want this, I want it to look like:

<img src="../../assets/add.svg" width="18" height="18"> 

but that works like:

<button type="button" class="btn_img" (click)="addItem()">
  <img src="../../assets/add.svg" width="18" height="18"> 
 </button>

How I can get this, how do you do this in angular?

You can see a working example here with all the possible ways:

https://stackblitz.com/edit/angular-kvsjre?file=src%2Fapp%2Fapp.component.html

If you like Use a < div > wrapper or an anchor < a > or an image

Good luck

Clickable image with Angular 2.x, In Angular 2/4 for event binding you use <a (click)="myTypeScriptFunction()"> </a>. This is documented here: https://angular.io/docs. Angular : Updating model doesnt work on consecutive click events 1 Pass data within a function parameter from HTML to typescript class without any event binding

I had a similar issue. It would work in Firefox and Internet Explorer but not in Chrome. For chrome, if all elements inside the button should respond to the click add this:

button > * {
    pointer-events: none;
}

Otherwise add to select elements as needed.

original answer from css-tricks

How to Programmatically Trigger 'Click' Event in Angular 4 & 5, The Component. The TS file is named uploadFile.component.ts. Upload Image. Important stuff: The method onFileChange($event) displays the  Angular Event binding - Button Click Usually, In any angular applications, User clicks on the button for form submission or navigation from one page to other using clicking button. Internally when the button is clicked, You need to handle some logic for form submission or navigate to some other page.

you can the click im image this way

in your view create img and input

<img [src]="image" alt="" class="image-exercise" (click)="selectFile.click()">
<input type="file" (change)="getImage($event)" style="display: none" #selectFile>

and your component create the function to save your image

getImage(ev) {
   console.log(ev.target.files[0]);
}

Angular 4 - Event Binding, Angular 4 - Event Binding - In this chapter, we will discuss how Event Binding works This is where event binding comes into picture. In the app.component.​html file, we have defined a button and added a function to it using the click event​. In this chapter, we will discuss how Event Binding works in Angular 4. When a user interacts with an application in the form of a keyboard movement, a mouse click, or a mouseover, it generates an event. These events need to be handled to perform some kind of action. This is where event binding comes into picture.

Angular ng-click Directive, An expression to execute when an element is clicked. More examples. Example. Execute a function, in AngularJS, when a button is clicked: <  Angular-cli includes the assets folder in the build options by default. I got this issue when the name of my images had spaces or dashes. For example : 'my-image-name.png' should be 'myImageName.png' 'my image name.png' should be 'myImageName.png' If you put the image in the assets/img folder, then this line of code should work in your templates :

Angular HTML Events, AngularJS Events. You can add AngularJS event listeners to your HTML elements by using one or more of these directives: ng-blur; ng-change; ng-click; ng-  Adding An Image to the Assets Folder. The assets folder that the Angular CLI generated for us is the perfect place to use for storing images. Let's go grab the Angular logo from angular.io. Go ahead and save that to our assets folder inside of an img folder.

ng-click and ng-if in Angular2 (click and *ngIf), Following up on our series, we're going to dive into ng-click and ng-if next. example, I went ahead and created our desired application in Angular 1.2. clicked, we add this function in our component.ts file (previously repeater.ts). We now have a cool Angular2 image clicking app that looks like this:. Create a Feature Component. Add In-app Navigation. Get Data from a Server. User actions such as clicking a link, pushing a button, and entering text raise DOM events. This page explains how to bind those events to component event handlers using the Angular event binding syntax. Run the live example / download example.

Comments
  • Have you tried wrapping the image in a div rather than a button to see if that works? And, adding the (click)="addItem()" to the div. Something like that. Bit of a guess.
  • I mean, you could use an <input type="image">, but I don't know how/if that would affect Angular.
  • (click) functionality works on images. There is likely something else causing the issue that isn't covered in your question or an error in your DOM that you haven't caught when adding that button. Working proof of (click) working on img: stackblitz.com/edit/github-hibfhb
  • It works for me when putting it like OP did and putting the <img> inside a <div> or <a>. Maybe the method was written wrong? That happens to me a lot.
  • Any Update if solved the problem?
  • Hmmm... Maybe it would be better to write that as a comment (with a link to CSS-tricks). But as a new user, I know that you don't have enough points for that yet. Hmm, hmm, hmm...