小编Bha*_*ani的帖子

主机绑定和主机监听

如何在angular 2中使用主机监听器和主机绑定?我尝试使用下面的主机监听器,但它总是显示Declaration expected错误.

app.component.ts:

import {Component, EventEmitter, HostListener, Directive} from 'angular2/core';

@Directive({
    selector: 'button[counting]'
})

class HostSample {
    public click = new EventEmitter();
    @HostListener('click', ['$event.target']);
    onClickBtn(btn){
        alert('host listener');
    }
}

@Component({
    selector: 'test',
    template: '<button counting></button>',
    directives: [HostSample]
})

export class AppComponent {
   constructor(){
   }
}
Run Code Online (Sandbox Code Playgroud)

angular

37
推荐指数
2
解决办法
10万
查看次数

标签 统计

angular ×1