简体   繁体   English

Angular 2+如何通过同级组件中的mouseleave事件触发切换指令

[英]Angular 2+ How can I trigger a toggle directive by a mouseleave event in a sibling component

When the user's mouse enters the yellow square (found in the yellow component) the appToggle directive is triggered (making isActive = true) changing the squares background to grey, this works . 当用户的鼠标进入黄色方块(位于黄色组件中)时,会触发appToggle指令(使isActive = true),将方块背景更改为灰色,即可正常工作

However I would like to be able to trigger the directive via the sibling component (the blue square found in the blue component) making (isActive = false) when the user's mouseleave's the blue square. 但是,我希望能够通过同级组件(​​在蓝色组件中找到蓝色方块)来触发指令(isActive = false),当用户的鼠标左键为蓝色方块时。

Please see stackblitz code example showing the problem . 请参阅显示问题的stackblitz代码示例

toggle.directive.ts toggle.directive.ts

  @HostListener('mouseleave', ['$event'])
  onMouseLeave(event) {
    if (event.target.classList.contains('blue')) {
      this.isActive = false;
    }
  }

The problem is that... 问题是...

event.target.classList.contains('blue')

is being completely ignored, I get no errors, but nothing is actually happening. 被完全忽略,我没有收到任何错误,但实际上没有任何反应。

I have searched everywhere to find a similar example which might solve this problem but have not been able to. 我到处搜索以找到类似的示例,它可以解决此问题,但未能成功。

Any help would be greatly appreciated, many thanks in advance folks! 任何帮助将不胜感激,在此先感谢大家!

The directive you applied knows nothing about the blue square. 您应用的指令对蓝色方块一无所知。

From the docs: 从文档:

The @HostListener decorator lets you subscribe to events of the DOM element that hosts an attribute directive

https://angular.io/guide/attribute-directives#directives-overview https://angular.io/guide/attribute-directives#directives-overview

So event.target in your case holds a reference to the yellow square 所以您的情况下的event.target拥有对黄色正方形的引用

You can achieve the desired effect via multiple paths, but most reasonable is probably to create two directives: 您可以通过多个路径来达到预期的效果,但是最合理的做法可能是创建两个指令:

appToggle - should toggle its state on mouse enter appToggle应在鼠标进入时切换其状态

appToggleSwitch - should accept a reference( https://angular.io/guide/template-syntax#ref-vars ) to the component it needs to change on some event: appToggleSwitch应该接受对某些事件需要更改的组件的引用( https://angular.io/guide/template-syntax#ref-vars ):

appToggleSwitch directive: appToggleSwitch指令:

  @Input('appToggleSwitch') componentToToggle: any;

  @HostListener('mouseleave', ['$event'])
  onMouseLeave(event) {
    this.componentToToggle.classList.add('grey');
  }

App component: 应用程序组件:

<div class="yellow" appToggle #yellow></div>
<div class="blue" [appToggleSwitch]=yellow></div>

I found a working solution, mouseout which makes use of mouse event bubbling. 我找到了一个可行的解决方案mouseout ,它利用了鼠标事件冒泡。 Code changed to the following... 代码更改为以下内容...

@HostListener('document:mouseout', ['$event'])
  onMouseOut(event) {
    if (event.target.classList.contains('blue')) {
      this.isActive = false;
    }
  }

For the sake of brevity and should anyone else be interested I also updated stackblitz with the working solution 为了简洁起见,如果还有其他人感兴趣,我还使用工作解决方案更新了stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM