【发布时间】:2019-06-15 12:02:30
【问题描述】:
现在点击汉堡菜单我得到下拉列表而不是鼠标悬停在汉堡菜单上时需要它这里是堆栈闪电战link。
【问题讨论】:
标签: angular angular-material angular7
现在点击汉堡菜单我得到下拉列表而不是鼠标悬停在汉堡菜单上时需要它这里是堆栈闪电战link。
【问题讨论】:
标签: angular angular-material angular7
您可以使用matMenuTrigger 指令来做到这一点
<button mat-icon-button [matMenuTriggerFor]="menu"
#menuTrigger="matMenuTrigger" (mouseenter)="menuTrigger.openMenu()">
要隐藏菜单,请为菜单添加mouseleave 事件。
将所有菜单项捆绑在 span 或 div 标记中。然后将(mouseleave)事件附加到它
<mat-menu #menu="matMenu" [overlapTrigger]="false">
<span (mouseleave)="menuTrigger.closeMenu()">
<button mat-menu-item>
<mat-icon>home</mat-icon>
<span>Home</span>
........
<mat-icon>exit_to_app</mat-icon>
<span>Logout</span>
</button>
</span>
</mat-menu>
分叉DEMO
【讨论】:
(mouseleave) 添加另一个事件绑定
我知道我参加聚会很晚了,但以上都没有真正对我有用。我最终写了一个指令,所以为我解决这个问题。
HoverDropDownDirective
import { NgModule } from '@angular/core';
import { Directive, Input, ElementRef, OnInit } from '@angular/core';
import { MatMenuTrigger, _MatMenu } from '@angular/material';
@Directive({
selector: '[hoverDropDown]'
})
export class HoverDropDownDirective implements OnInit {
isInHoverBlock = false;
constructor(private el: ElementRef) {}
@Input() hoverTrigger: MatMenuTrigger;
@Input() menu: any;
ngOnInit() {
this.el.nativeElement.addEventListener('mouseenter', () => {
this.setHoverState(true);
this.hoverTrigger.openMenu();
const openMenu = document.querySelector(`.mat-menu-after.${this.menu._elementRef.nativeElement.className}`);
if (!openMenu) {
this.hoverTrigger.closeMenu();
return;
}
openMenu.addEventListener('mouseenter', () => {
this.setHoverState(true);
});
openMenu.addEventListener('mouseleave', () => {
this.setHoverState(false);
});
});
this.el.nativeElement.addEventListener('mouseleave', () => {
this.setHoverState(false);
});
}
private setHoverState(isInBlock: boolean) {
this.isInHoverBlock = isInBlock;
if (!isInBlock) {
this.checkHover();
}
}
private checkHover() {
setTimeout(() => {
if (!this.isInHoverBlock && this.hoverTrigger.menuOpen) {
this.hoverTrigger.closeMenu();
}
}, 50);
}
}
@NgModule({
declarations: [
HoverDropDownDirective
],
exports: [
HoverDropDownDirective
]
})
export class HoverDropDownDirectiveModule {}
app.module
import { HoverDropDownDirectiveModule } from '../../directives/hover-drop-down.directive';
imports: [
HoverDropDownDirectiveModule
]
HTML
<div *ngFor="let category of categories">
<button #menuTrigger="matMenuTrigger"
mat-button
[matMenuTriggerFor]="children"
(click)="navigateMain(category.Category)"
hoverDropDown
[menu]="children"
[hoverTrigger]="menuTrigger">
{{category.Category.Description}}
</button>
<mat-menu #children="matMenu" hasBackdrop="false">
<button mat-menu-item *ngFor="let sub of category.SubCategories" (click)="navigateSubCategory(sub)">{{sub.Description}}</button>
</mat-menu>
</div>
需要注意的两件事:
hasBackdrop="false" mat-menu 中的属性hasBackdrop="false" 属性记录在 Angular Material 上。希望这对你有用...
【讨论】:
为 mat 按钮添加一个引用变量,用于在 mouseover 事件上触发 click。
<button mat-icon-button
#matBtn
(mouseover)="matBtn._elementRef.nativeElement.click()"
[matMenuTriggerFor]="menu"
>
注意:我真的不喜欢像这样访问对象的“私有”属性,我的这个解决方案更像是一种解决方法,如果你没有找到其他解决方案,请使用它。
【讨论】:
MatMenuTrigger
该指令旨在与 mat-menu 一起使用 标签。它负责切换提供的菜单的显示 实例。
<button #r="matMenuTrigger" mat-icon-button [matMenuTriggerFor]="menu" >
<mat-icon (mouseover)="open(r)" >menu</mat-icon>
</button>
例如:https://stackblitz.com/edit/example-angular-material-toolbar-menu-wrut3v
【讨论】:
覆盖mouseover 事件并创建#menuTrigger 引用变量。这将解决您的问题。
<button mat-icon-button [matMenuTriggerFor]="menu" #menuTrigger="matMenuTrigger"
(mouseover)="menuTrigger.openMenu()" >
我已经更新了你的stackblitz
【讨论】:
app.component.html:
<mat-toolbar color="primary" >
<span class="fill-remaining-space">
<button #clickMe mat-icon-button [matMenuTriggerFor]="menu" (mouseenter)="clickOnHover()">
<mat-icon>menu</mat-icon>
</button>
<mat-menu #menu="matMenu" [overlapTrigger]="false">
<button mat-menu-item>
<mat-icon>home</mat-icon>
<span>Home</span>
</button>
<button mat-menu-item>
<mat-icon>people_outline</mat-icon>
<span>Connecting</span>
</button>
<button mat-menu-item>
<mat-icon>videocam</mat-icon>
<span>Let's talk</span>
</button>
<button mat-menu-item>
<mat-icon>exit_to_app</mat-icon>
<span>Logout</span>
</button>
</mat-menu>
</span>
<span class="fill-remaining-space">Application Title</span>
</mat-toolbar>
app.component.ts:
import { Component, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
@ViewChild('clickMe') clickMe: any;
clickOnHover() {
this.clickMe._elementRef.nativeElement.click();
}
}
【讨论】:
希望对你有帮助
使用(鼠标输入)功能 Angular-hover-stackblitz
【讨论】: