[英]Conditionally bind an event in Aurelia
Is it possible to conditionally bind an event using .trigger()
or .delegate()
in the html view? 是否可以在html视图中使用
.trigger()
或.delegate()
有条件地绑定事件?
This is the regular way: 这是常规方式:
<div mousedown.delegate="handleMouseDown($event)"></div>
I was thinking of something like: 我想的是:
<div mousedown.delegate="isDraggable ? handleMouseDown($event) : null"></div>
Ideally if isDraggable === false
no handler is registered. 理想情况下,如果
isDraggable === false
不会注册处理程序。
Currently I'm just doing this check in the view model attached()
function with .addEventListener()
is there a better way? 目前我只是在视图模型
attached()
函数中进行此检查.addEventListener()
有更好的方法吗?
As @thebluefox pointed out in the question comments 正如@thebluefox在问题评论中指出的那样
<div mousedown.delegate="isDraggable ? handleMouseDown($event) : null"></div>
does work however there are some issues when adding in a binding behavior. 确实有效但添加绑定行为时会出现一些问题。
The following examples will fail when evaluating the ternary 在评估三元时,以下示例将失败
<div mousedown.delegate="isDraggable ? handleMouseDown($event) & throttle:500 : null"></div>
<div mousedown.delegate="isDraggable ? (handleMouseDown($event) & throttle:500) : null"></div>
As a workaround you can invert the test so the binding is last eg 作为一种解决方法,您可以反转测试,以便绑定是最后的例如
<div mousemove.delegate="!isDraggable ? null : handleMouseMove($event) & throttle:500 "></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.