Angular 2 HostListener & HostBinding

大綱

1、宿主元素(Host Element)
2、HostListener
3、HostListenerDecorator 裝飾器應(yīng)用
4、HostBinding
5、HostBinding 裝飾器應(yīng)用

宿主元素(Host Element)

在介紹 HostListener 和 HostBinding 屬性裝飾器之前,我們先來(lái)了解一下 host element (宿主元素)。
宿主元素的概念同時(shí)適用于指令和組件。對(duì)于指令來(lái)說(shuō),這個(gè)概念是相當(dāng)簡(jiǎn)單的。應(yīng)用指令的元素,就是宿主元素。假設(shè)我們已聲明了一個(gè) HighlightDirective 指令 (selector: '[exeHighlight]'):

<p exeHighlight>
   <span>高亮的文本</span>
</p>

上面 html 代碼中,p 元素就是宿主元素。如果該指令應(yīng)用于自定義組件中如:

<exe-counter exeHighlight>
    <span>高亮的文本</span>
</exe-counter>

此時(shí) exe-counter 自定義元素,就是宿主元素。

HostListener

HostListener 是屬性裝飾器,用來(lái)為宿主元素添加事件監(jiān)聽。

/*
  HostListenerDecorator 裝飾器定義
*/
export interface HostListenerDecorator {
    (eventName: string, args?: string[]): any;
    new (eventName: string, args?: string[]): any;
}
HostListenerDecorator 裝飾器應(yīng)用
/*
    counting.directive.ts
*/
import { Directive, HostListener } from '@angular/core';

@Directive({
    selector: 'button[counting]'
})
class CountClicks {
    numberOfClicks = 0;

    @HostListener('click', ['$event.target'])
    onClick(btn: HTMLElement) {
        console.log('button', btn, 'number of clicks:', this.numberOfClicks++);
    }
}
/*
    app.component.ts
*/
import { Component} from '@angular/core';

@Component({
  selector: 'exe-app',
  styles: [`
    button {
      background: blue;
      color: white;
      border: 1px solid #eee;
    }
  `],
  template: `
    <button counting>增加點(diǎn)擊次數(shù)</button>
  `
})
export class AppComponent {}

/*
    以上代碼運(yùn)行后瀏覽器顯示的結(jié)果:
*/
HostListenerDecorator 裝飾器應(yīng)用

此外,我們也可以監(jiān)聽宿主元素外,其它對(duì)象產(chǎn)生的事件,如 window 或 document 對(duì)象。具體示例如下:

/*
    highlight.directive.ts
*/
import { Directive, HostListener, ElementRef, Renderer } from '@angular/core';

@Directive({
    selector: '[exeHighlight]'
})
export class ExeHighlight {
    constructor(private el: ElementRef, private renderer: Renderer) { }

    @HostListener('document:click', ['$event'])
    onClick(btn: Event) {
        if (this.el.nativeElement.contains(event.target)) {
            this.highlight('yellow');
        } else {
            this.highlight(null);
        }
    }

    highlight(color: string) {
        this.renderer.setElementStyle(this.el.nativeElement, 'backgroundColor', color);
    }
}

/*
    app.component.ts
*/
import { Component} from '@angular/core';

@Component({
  selector: 'exe-app',
  template: `
    <h4 exeHighlight>點(diǎn)擊該區(qū)域,元素會(huì)被高亮。點(diǎn)擊其它區(qū)域,元素會(huì)取消高亮</h4>
  `
})
export class AppComponent {}

/*
    以上代碼運(yùn)行后瀏覽器顯示的結(jié)果:
*/
HostListener

我們也可以在指令的 metadata 信息中,設(shè)定宿主元素的事件監(jiān)聽信息,具體示例如下:

/*
    counting.directive.ts
*/
import { Directive } from '@angular/core';

@Directive({
    selector: 'button[counting]',
    host: {
      '(click)': 'onClick($event.target)'
    }
})
export class CountClicks {
    numberOfClicks = 0;

    onClick(btn: HTMLElement) {
        console.log('button', btn, 'number of clicks:', this.numberOfClicks++);
    }
}
HostBinding

HostBinding 是屬性裝飾器,用來(lái)動(dòng)態(tài)設(shè)置宿主元素的屬性值。

/*
    HostBinding 裝飾器定義
*/
export interface HostBindingDecorator {
    (hostPropertyName?: string): any;
    new (hostPropertyName?: string): any;
}
HostBinding 裝飾器應(yīng)用
/*
    button-press.directive.ts
*/
import { Directive, HostBinding, HostListener } from '@angular/core';

@Directive({
    selector: '[exeButtonPress]'
})
export class ExeButtonPress {
    @HostBinding('attr.role') role = 'button';
    @HostBinding('class.pressed') isPressed: boolean;

    @HostListener('mousedown') hasPressed() {
        this.isPressed = true;
    }
    @HostListener('mouseup') hasReleased() {
        this.isPressed = false;
    }
}

/*
    app.component.ts
*/
import { Component } from '@angular/core';

@Component({
  selector: 'exe-app',
  styles: [`
    button {
      background: blue;
      color: white;
      border: 1px solid #eee;
    }
    button.pressed {
      background: red;
    }
  `],
  template: `
    <button exeButtonPress>按下按鈕</button>
  `
})
export class AppComponent { }

/*
    以上代碼運(yùn)行后瀏覽器顯示的結(jié)果:
*/
HostBinding 裝飾器應(yīng)用

HostBinding 裝飾器應(yīng)用
Host Property Bindings

我們也可以在指令的 metadata 信息中,設(shè)定宿主元素的屬性綁定信息,具體示例如下:

/*
    button-press.directive.ts
*/
import { Directive, HostListener } from '@angular/core';

@Directive({
    selector: '[exeButtonPress]',
    host: {
      'role': 'button',
      '[class.pressed]': 'isPressed'
    }
})
export class ExeButtonPress {
    isPressed: boolean;

    @HostListener('mousedown') hasPressed() {
        this.isPressed = true;
    }
    @HostListener('mouseup') hasReleased() {
        this.isPressed = false;
    }
}
宿主元素屬性和事件綁定風(fēng)格指南

優(yōu)先使用 @HostListener 和 @HostBinding ,而不是 @Directive 和 @Component 裝飾器的 host 屬性。
對(duì)于關(guān)聯(lián)到 @HostBinding 的屬性或關(guān)聯(lián)到 @HostListener 的方法,要修改時(shí),只需在指令類中的一個(gè)地方修改。 如果使用元數(shù)據(jù)屬性 host,你就得在組件類中修改屬性聲明的同時(shí)修改相關(guān)的元數(shù)據(jù)。

參考網(wǎng)址

https://segmentfault.com/a/1190000008878888

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評(píng)論 19 139
  • 學(xué)習(xí)資料來(lái)自 Angular.cn 與 Angular.io。 模板語(yǔ)法 在線例子 在 Angular 中,組件扮...
    小鐳Ra閱讀 4,008評(píng)論 0 3
  • 今年已90歲高壽的鐵路局離休老干部張慶生前天在醫(yī)院去世。這是聽居委干部說(shuō)的。 我們與老張比較熟悉。在天熱的時(shí)候,總...
    老樂(lè)銘閱讀 469評(píng)論 0 1
  • 汪星人,到我這來(lái)和我合張影吧,看這架勢(shì),趕腳是做好了自拍的準(zhǔn)備,這是準(zhǔn)備帥別人一臉的感覺(jué)嘛?可愛(ài)又不失一種紳士范,...
    娛樂(lè)快訊閱讀 230評(píng)論 0 0

友情鏈接更多精彩內(nèi)容