Angular4 @HostBinding @HostListener
host属性@Component({ selector: 'jhi-project', templateUrl: './project.html', styleUrls: [],
}) export class JhiProjectComponent { keyboardInput(event) { if (event.keyCode == 65 && event.ctrlKey) { // ctrl + a .... } } } @HostListenerHostListener是属性装饰器,用来为宿主元素添加事件监听。 定义:
export ,args?: (eventName: ,args?:
应用:
import { Directive,HostListener } @Directive({
selector: <span style="color: #800000">'<span style="color: #800000">button[counting]<span style="color: #800000">'<span style="color: #000000"> }) <span style="color: #0000ff">class<span style="color: #000000"> CountClicks { numberOfClicks = <span style="color: #800080">0<span style="color: #000000">;
} app.component.ts import { Component} @Component({
selector: <span style="color: #800000">'<span style="color: #800000">exe-app<span style="color: #800000">'<span style="color: #000000">,styles: [ button { background: blue; color: white; border: 1px solid #eee; } ],template: </span><button counting>增加点击次数</button><span style="color: #000000"> }) export <span style="color: #0000ff">class AppComponent {} 以上代码运行结果: 此外,还可以监听宿主元素外,其他对象产生的事件,如windown或document对象。 highlight.directive.ts import { Directive,HostListener,ElementRef,Renderer } @Directive({
selector: <span style="color: #800000">'<span style="color: #800000">[exeHighlight]<span style="color: #800000">'<span style="color: #000000"> }) export <span style="color: #0000ff">class<span style="color: #000000"> ExeHighlight { constructor(<span style="color: #0000ff">private el: ElementRef,<span style="color: #0000ff">private<span style="color: #000000"> renderer: Renderer) { }
} import {HostListener} @HostListener(<span style="color: #800000">'<span style="color: #800000">window:keydown<span style="color: #800000">',[<span style="color: #800000">'<span style="color: #800000">$event<span style="color: #800000">'<span style="color: #000000">])
onKeyDown(<span style="color: #0000ff">event<span style="color: #000000">: KeyboardEvent) { ... } app.component.ts import { Component} @Component({
selector: <span style="color: #800000">'<span style="color: #800000">exe-app<span style="color: #800000">'<span style="color: #000000">,template: </span><h4 exeHighlight>点击该区域,元素会被高亮。点击其它区域,元素会取消高亮</h4><span style="color: #000000"> }) export <span style="color: #0000ff">class AppComponent {} 也可以在指定的metadata信息中,设定宿主元素的事件监听信息,示例: counting.directive.ts import { Directive } @Directive({
selector: <span style="color: #800000">'<span style="color: #800000">button[counting]<span style="color: #800000">'<span style="color: #000000">,host: { <span style="color: #800000">'<span style="color: #800000">(click)<span style="color: #800000">': <span style="color: #800000">'<span style="color: #800000">onClick($event.target)<span style="color: #800000">'<span style="color: #000000"> } }) export <span style="color: #0000ff">class<span style="color: #000000"> CountClicks { numberOfClicks = <span style="color: #800080">0<span style="color: #000000">;
} @HostBindingHostBinding 是属性装饰器,用来动态设置宿主元素的属性值。 定义: export ?: (hostPropertyName?:
应用: @HostBindings() foo =
button-press.directive.ts import { Directive,HostBinding,HostListener } @Directive({
selector: <span style="color: #800000">'<span style="color: #800000">[exeButtonPress]<span style="color: #800000">'<span style="color: #000000"> }) export <span style="color: #0000ff">class<span style="color: #000000"> ExeButtonPress { @HostBinding(<span style="color: #800000">'<span style="color: #800000">attr.role<span style="color: #800000">') role = <span style="color: #800000">'<span style="color: #800000">button<span style="color: #800000">'<span style="color: #000000">; @HostBinding(<span style="color: #800000">'<span style="color: #800000">class.pressed<span style="color: #800000">'<span style="color: #000000">) isPressed: boolean;
} app.component.ts import { Component } @Component({
selector: <span style="color: #800000">'<span style="color: #800000">exe-app<span style="color: #800000">'<span style="color: #000000">,styles: [ button { background: blue; color: white; border: 1px solid #eee; } button.pressed { background: red; } ],template: </span><button exeButtonPress>按下按钮</button><span style="color: #000000"> }) export <span style="color: #0000ff">class AppComponent { } 我们也可以在指令的 metadata 信息中,设定宿主元素的属性绑定信息,具体示例如下: button-press.directive.ts import { Directive,HostListener } @Directive({ selector: <span style="color: #800000">'<span style="color: #800000">[exeButtonPress]<span style="color: #800000">'<span style="color: #000000">,host: { <span style="color: #800000">'<span style="color: #800000">role<span style="color: #800000">': <span style="color: #800000">'<span style="color: #800000">button<span style="color: #800000">'<span style="color: #000000">,<span style="color: #800000">'<span style="color: #800000">[class.pressed]<span style="color: #800000">': <span style="color: #800000">'<span style="color: #800000">isPressed<span style="color: #800000">'<span style="color: #000000"> } }) export <span style="color: #0000ff">class<span style="color: #000000"> ExeButtonPress { isPressed: boolean;
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |