美文网首页
@HostBinding and @HostListener:

@HostBinding and @HostListener:

作者: 张霸天 | 来源:发表于2017-05-26 18:45 被阅读0次

    Here is a basic hover example.

    Component's template property:

    Template

    <!-- attention, we have the c_highlight class -->
    <!-- c_highlight is the selector property value of the directive -->
    
    <p class="c_highlight">
        Some text.
    </p>
    
    

    And our directive

    import {Component,HostListener,Directive,HostBinding} from '@angular/core';
    
    @Directive({
        // this directive will work only if the DOM el has the c_highlight class
        selector: '.c_highlight'
     })
    export class HostDirective {
    
      // we could pass lots of thing to the HostBinding function. 
      // like class.valid or attr.required etc.
    
      @HostBinding('style.backgroundColor') c_colorrr = "red"; 
    
      @HostListener('mouseenter') c_onEnterrr() {
       this.c_colorrr= "blue" ;
      }
    
      @HostListener('mouseleave') c_onLeaveee() {
       this.c_colorrr = "yellow" ;
      } 
    }
    
    

    相关文章

      网友评论

          本文标题:@HostBinding and @HostListener:

          本文链接:https://www.haomeiwen.com/subject/rebxfxtx.html