0

Angular 2: Get reference to a directive used in a component

I want to change img src on hover.for reference go through this link:--

3

1 Answer 1

3

You can use mouseenter and mouseout on host element as shown below,

Look at working demo : https://plnkr.co/edit/GfgZ46?p=preview

 //our root app component import {Component, Directive, Output, EventEmitter, Input, SimpleChange} from 'angular2/core' @Component({ selector: 'my-app', host: { '(mouseenter)':'MouseEnter()' '(mouseout)':'MouseOut()' }, template: ` <img [src]="source"/> ` }) export class App { source='images/angular.png'; MouseEnter(){ console.log('Hovering'); this.source = 'images/car.png'; } MouseOut(){ this.source='images/angular.png'; } } 
Sign up to request clarification or add additional context in comments.

10 Comments

my HTML:--<img class="badge_alignment" [src]="source" width="50px" height="50px" on-mouseover='MouseEnter()' on-mouseout='MouseOut()' > is that a correct way to change image source on hover?
what is on-mouseover and on-mouseout?
My Html;--<img _ngcontent-luk-5="" class="badge_alignment" height="50px" width="50px" src="assets/img/{{key}}_inactive.png"> Component.ts;-- public source='assets/img/{{key}}_inactive.png'; MouseEnter(){ console.log('Hovering'); this.source = 'assets/img/{{key}}_active.png'; } MouseOut(){ console.log('mouseout'); this.source='assets/img/{{key}}_inactive.png'; } but the source is not going to bind and the image is not going to display.
In HTML i give a images [src]="source" and Define the source in component.ts like that public source="assets/img/{{key}}_inactive.png"; but when i am going to display my page for images it shows 404 not found error. How can i solve this.
Which Angular2 version are you using?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.