我知道它必须是非常简单的东西,但今天无法想象 - 周一:)
所以,我有一个响应表的指令
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();
}
}
这是我的观点
<div class="table-responsive">
<a href="#" (click)="scroll('left', $event)">LEFT</a>
<a href="#" (click)="scroll('right', $event)">RIGHT</a>
<table>...</table>
</div>
现在,我该如何让它发挥作用?我不想捕捉整个.table-responsive ......但只是那两个链接
答案 0 :(得分:2)
不想深入研究,但这将是一个快速的解决方案。尝试将您的左,右信息与事件绑定,将其绑定到像这样的事件
<div class="table-responsive">
<a href="#" (click)="$event.left = true">LEFT</a>
<a href="#" (click)="$event.right = true">RIGHT</a>
<table>...</table>
</div>
像这样提取它
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();
}
}
在这种情况下,你可以通过事件对象中的左,右标志简单地知道方向并添加你的代码:)
答案 1 :(得分:1)
感谢@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();
}
}
}
和视图
<div class="table-responsive">
<a href="#" (click)="$event.direction='left'">LEFT</a>
<a href="#" (click)="$event.direction='right'">RIGHT</a>
</div>