【问题标题】:angular2+: Toggle between click and mouseenterangular2+:在 click 和 mouseenter 之间切换
【发布时间】:2019-02-23 09:30:36
【问题描述】:

我正在为我的应用菜单构建一个组件。 当菜单处于紧凑模式时需要在 mouseenter 上打开子菜单,而在宽模式下需要在单击时打开子菜单(两者都是组件内“nav”元素的 css 类)。

<nav class="{{menuState}}">
    <ul>
        <li *ngFor="let child of menuitem.children; let i = index" class="menu-item" [ngClass]="{'display-menu': child.subOpen === true, '' : child.subOpen === false}" (mouseenter)="child.subOpen=true" (mouseleave)="child.subOpen=false" (click)="child.subOpen=true"></li>
    </ul>
<nav>

如何使 mouseenter / click 事件仅在包装导航元素具有相关类时触发?

【问题讨论】:

    标签: javascript angular angular-cli-v7


    【解决方案1】:

    您可以在组件类中创建一个 evaluateEvent 函数,该函数可以根据分配给菜单项的类名注册事件。添加评估事件的引用 (单击)和(鼠标悬停)事件的函数并传入 $event 并传入菜单的类名。(假设您根据紧凑和宽版本向菜单附加不同的类名)。

    这里是sn-p。 (我添加了一个用于切换紧凑型和宽型版本的按钮)。事件绑定在 HelloAngular js 按钮上,具体取决于版本 - 紧凑或宽

    stackblitz 链接 - https://stackblitz.com/edit/angular-vmpivu 文件 -hello.component.ts

                        import { Component, Input } from '@angular/core';
                        @Component({
                        selector: 'hello',
                        template: `<button (click)="evaluateEvent($event,classType)" 
                                   (mouseover)="evaluateEvent($event,classType)" 
                                   [ngClass]="classType"> Hello {{name}}!</button><br/>
                           <br/>
                        <button (click)="toggleClass($event)">Toggle Class 
                        ({{this.classType}}--{{this.text}})</button>
                        `,
                        styles: [`h1 { font-family: Lato; }`]
                        })
                        export class HelloComponent  {
                        @Input() name: string;
                        classType:String="compact";
                        text:String="mouseover bind";
                        toggleClass(ev)
                        {
                            if(this.classType==='compact'){this.classType= 
                            'wide';this.text="click bind"}
                            else if(this.classType==='wide'){this.classType= 
                            'compact';this.text="mouseover bind"}
                        }
                        compactHandler()
                        {
                            alert('Hi am a compact handler');
                        }
                        widehandler()
                        {
                            alert('Hi am a wide handler');
                        }
    
                        evaluateEvent(ev:any,classType){
                            if((ev.type==="click")&&(classType==="wide")){
                                return this.compactHandler();
                            }
                            else if((ev.type==="mouseover")&&(classType==="compact")){
                                return this.widehandler();
                            }
                        } 
                        }
    

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2016-06-22
      • 1970-01-01
      • 2019-01-11
      • 2010-10-28
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多