【发布时间】:2018-10-27 08:15:53
【问题描述】:
我正在尝试禁用 div 内的所有点击事件。我禁用了整个 div,但这对我来说不是解决方案,因为我有 mouseEnter 事件需要继续工作。
问题是它是我在应用程序的不同部分使用的指令,所以这个 div 有不同的结构,有时按钮是“直接子”,有时它在第二级。
这是我的尝试,它有效,但我发现它“脏”:
<div #mydirective>
<button (click)="function1()">button</button>
<div>
<a (click)="function2()">link</a>
</div>
</div>
//directive ts
var children = this.element.nativeElement.children;
for (let child of children) {
this.renderer.setAttribute(child, 'disabled', 'true'); //for button
this.renderer.addClass(child, 'disabled'); //for links
var grandchildren = child.children;
for (let grandchild of grandchildren) {
this.renderer.setAttribute(grandchild, 'disabled', 'true');
this.renderer.addClass(grandchild, 'disabled');
}
}
我相信它应该更容易,有什么方法可以在我的 div 中找到所有按钮和链接?
【问题讨论】:
-
为什么不使用表单。这样你就可以设置验证逻辑来决定表单内的按钮是否被禁用?我假设你也有一些意见。
-
它需要对我的代码进行大量更改,使用指令的想法是尽可能减少更改。但是,如果我找不到其他选项,我可以选择此选项。谢谢!
标签: angular angular5 angular2-directives angular-directive