How to use mouseover and mouseout in Angular 6

后端 未结 8 1048
既然无缘
既然无缘 2020-12-14 00:09

I have this older Angular code which works but not in the latest version of Angular 6.

8条回答
  •  星月不相逢
    2020-12-14 00:30

    If your interested , then go with directive property . Code might looks bit tough , but itshows all the property of Angular 6 . Here am adding a sample code

    import { Directive, OnInit, ElementRef, Renderer2 ,HostListener,HostBinding,Input} from '@angular/core';
    import { MockNgModuleResolver } from '@angular/compiler/testing';
    //import { Event } from '@angular/router';
    
    @Directive({
      selector: '[appBetterHighlight]'
    })
    export class BetterHighlightDirective implements OnInit {
       defaultcolor :string = 'black'
       Highlightedcolor : string = 'red'
        @HostBinding('style.color') color : string = this.defaultcolor;
    
      constructor(private elm : ElementRef , private render:Renderer2) { }
    ngOnInit()
    {}
    @HostListener('mouseenter') mouseover(event :Event)
    {
    
      this.color= this.Highlightedcolor ;
    }
    @HostListener('mouseleave') mouseleave(event: Event)
    {
    
      this.color = this.defaultcolor;
    }
    }
    

    Just use the selector name 'appBetterHighlight' anywhere in the template to access this property .

提交回复
热议问题