赞
踩
在 Angular 中,我们可以使用 HostListener 属性装饰器,实现元素的事件绑定。
该指令用于演示如何利用 HostListener 装饰器,监听用户的点击事件。
import { Directive, HostBinding, HostListener, Input } from '@angular/core';
@Directive({
selector: '[greet]' }) export class GreetDirective {
@Input() greet: string;
@HostBinding()
get innerText() {
return this.greet;
} @HostListener('click',['$event'])
onClick(event) { this.greet = 'Clicked!';
}
}
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h2>Hello, Angular</h2>
<h2 [greet]="'Hello, Semlinker!'">Hello, Angular</h2>
`,
})
export class AppComponent { }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。