2017-03-15 38 views
0

我需要同时支持鼠标和触摸事件,所以我有这样角2,如何编写自定义的mousedown/touchstart实施

<button 
     (touchstart)="doSomething($event)" 
     (mousedown)="doSomething($event)"> 

代码是有一些方法来都合并成一个?我还注意到触摸设备触发鼠标事件,所以我需要更好的解决方案。是否有可能,例如,上课我听这两个事件编写事件侦听器,像这样

<button (pointerDown)="doSomething">...</button> 

在我PointerDown事件?这将是很好的:)

我还发现ng2-events库,不知道它是否会做我所需要的。

任何帮助,将不胜感激!

回答

1

您可以使用属性指令和@HostListener完成所有操作。不要在我的脑海中拥有一切,但一旦你看到它,你会明白的

+0

非常感谢。这是否意味着你还必须添加属性,如:'

+0

是'@Output pointerDown = new EventEmitter()'和'pointerDown.emit(value)'应该调用您提供的回调函数 – Julian

+0

是的,我这样做了。对于那些感兴趣的人,[这里](https://angular.io/docs/ts/latest/guide/attribute-directives.html)是我用来弄清楚的文档 –