[英]Angular2: Directives: capturing click event on children
I know it must be something very simple, but cannot figure it today - Monday:)我知道这一定很简单,但今天无法理解 - 星期一:)
so, I have a directive for a responsive table所以,我有一个响应表的指令
import {Directive, HostListener} from '@angular/core';
@Directive({
selector: '.table-responsive'
})
export class TableResponsiveDirective {
@HostListener('click', ['$event']) scroll(direction: string, event: MouseEvent){
console.info('clicked: ' + direction + ' ' +event);
event.stopPropagation();
event.preventDefault();
}
}
and this is my view这是我的观点
<div class="table-responsive">
<a href="#" (click)="scroll('left', $event)">LEFT</a>
<a href="#" (click)="scroll('right', $event)">RIGHT</a>
<table>...</table>
</div>
Now, how do I make it work?现在,我该如何让它发挥作用? I don't want to capture the whole.table-responsive... but just those two links我不想捕获 whole.table-responsive ...但只是这两个链接
Don't want to go in depth but this will be a quick solution for you. 不想深入,但这将是一个快速的解决方案。 Try to bind your left, right information with event, bind it inside with event like this 尝试将您的左侧,右侧信息与事件绑定,将其绑定到此类事件中
<div class="table-responsive">
<a href="#" (click)="$event.left = true">LEFT</a>
<a href="#" (click)="$event.right = true">RIGHT</a>
<table>...</table>
</div>
extract it like this 像这样提取它
import {Directive, HostListener} from '@angular/core';
@Directive({
selector: '.table-responsive'
})
export class TableResponsiveDirective {
@HostListener('click', ['$event'])
scroll($event){
if ($event.left) {
console.info('clicked: ' + $event.left);
} else if ($event.right) {
console.info('clicked: ' + $event.right);
}
event.stopPropagation();
event.preventDefault();
}
}
In this case you can simply know the direction by left, right flags in event object and add your code :) 在这种情况下,您可以通过事件对象中的左,右标志简单地知道方向并添加您的代码:)
Thanks @Babar Bilal, based on your answer, I did a more polished solution (for my needs) 谢谢@Babar Bilal,根据你的回答,我做了一个更加精致的解决方案(根据我的需要)
import {Directive, HostListener} from '@angular/core';
interface tableMouseEvent extends MouseEvent{
direction: string
}
@Directive({
selector: '.table-responsive'
})
export class TableResponsiveDirective {
@HostListener('click', ['$event'])
scroll(event: tableMouseEvent){
if(event.direction){
console.info('clicked: ' + event.direction);
event.stopPropagation();
event.preventDefault();
}
}
}
and the view 和观点
<div class="table-responsive">
<a href="#" (click)="$event.direction='left'">LEFT</a>
<a href="#" (click)="$event.direction='right'">RIGHT</a>
</div>
In angular 8 and higher versions, the component template works like this:在 angular 8 及更高版本中,组件模板的工作方式如下:
<div class="table-responsive">
<a href="#" (click)="$any($event).direction='left'">LEFT</a>
<a href="#" (click)="$any($event).direction='right'">RIGHT</a>
</div>
Otherwise might run into the problem: error TS2339: Property 'direction' does not exist on type 'MouseEvent'
.否则可能会遇到问题: error TS2339: Property 'direction' does not exist on type 'MouseEvent'
。
The $any typecast function $any($event.target).value
is used to stop the type checking in the template. $any 类型转换 function $any($event.target).value
用于停止模板中的类型检查。 details angular doc 详情angular 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.