当前位置:   article > 正文

angular HostListener_angular 事件处理_angular 教程_angular视频教程 - angular 指令教程_@hostlistener('click',

@hostlistener('click',

转载自 http://www.ngui.cc/news/show-128.html


在 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 { }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/223051
推荐阅读
相关标签
  

闽ICP备14008679号