角度,恢复事件传播 [英] Angular, resume event propagation
问题描述
当用户单击按钮时,会有一条指令捕获该事件并停止它.然后打开一个模态,要求用户确认.如果用户确认,那么我需要恢复以前的事件.
When user click a button there is a directive that catches this event and stops it. Then an modal is opened witch asks for user confirmation. If user confirms then I need to resume previously event.
如何恢复已停止的事件?
示例:
markAsSeen($event) : void {
// pause whatever user wanted to click
$event.stopPropagation();
// open modal and ask user for confirmation
let modalInstance = this.modalService.openConfirmationModal();
// on modal close, if positive event continue whatever user clicked
modalInstance.onClose((response) => {
if(response) {
// this line should resume $event
$event.originalEvent(); // how to achieve this?
}
})
}
推荐答案
对我来说,这是两个不同的事件,第一个在这里是openModal,但看起来没用(为什么不打开一个模态?),第二个确认用户单击确认"的时间.
For me this is two different events, the first one is here to openModal but looks useless (why don't you just open a modal ?), the second one to confirm when the user clicked Confirm.
对我来说,这是最简单的方法:如果您需要第一个事件发射器,则仅打开模式,第二个如果是肯定的,则启动确认过程.另一种方法是在确认中添加"status"变量-1表示未启动(=模态关闭),1表示肯定确认,0正在进行.
For me that's the easiest way : if you need the first event emitter, then only open the modal, the second one start the confirmation process if positive. The other way could be to add a "status" variable in your confirmation -1 for not started (= modal closed), 1 for positive confirmation, 0 in progress.
最后,为避免用户点击鼠标,请使用
Finally to avoid user to click away, use something like
onClick(event) {
if (!this.element.nativeElement.contains(event.target)) {
closeModal(); // or not
}
}
其中event.target是单击的目标
Where event.target is the clicked target
onClick必须添加到@Component
@Component({selector..., host: {
'(document:click)': 'onClick($event)',
}});
这篇关于角度,恢复事件传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!