[英]How to display mat-menu after entering 3 characters in input field?
I'm implement this input field so that when I enter 3 characters I want to display mat-menu as a value of mat-option but for some reason the mat-menu never gets display.我正在实现这个输入字段,这样当我输入 3 个字符时,我想将 mat-menu 显示为 mat-option 的值,但由于某种原因,mat-menu 永远不会显示。 Can anyone point me in the right direction?
谁能指出我正确的方向? thanks a lot in advance!
提前非常感谢!
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngIf="isSpinnerLoading">
<div *ngIf="!menuContent">Loading...</div>
<div *ngIf="menuContent">
show menu
<mat-menu #menu="matMenu" class="menu-select">
<div (click)="$event.stopPropagation()">
<div *ngFor="let item of myList">
<mat-checkbox
class="mat-menu-item">
{{item}}
</mat-checkbox>
</div>
</div>
</mat-menu>
</div>
</mat-option>
</mat-autocomplete>
This is what I'm trying to accomplish:这就是我想要完成的事情:
The problem that you are facing is that the mat-menu
openMenu()
function is not triggered when your content opens您面临的问题是打开内容时未触发
mat-menu
openMenu()
function
We will need a way to trigger the menu to open Below is an approach that I have used我们需要一种方法来触发菜单打开下面是我使用的一种方法
Add the below markup in your html在您的 html 中添加以下标记
<span #menuTrigger="matMenuTrigger" [matMenuTriggerFor]="menu">show menu</span>
In your TS file add below @ViewChild
property to hold the matMenuTrigger
在您的 TS 文件中添加下面的
@ViewChild
属性以保存matMenuTrigger
@ViewChild("menuTrigger", { static: false }) set menuTrigger(
content: MatMenuTrigger
) {
if (content) {
this.contentPlaceholder = content;
}
}
Finally amend your onKeyUp()
function最后修改你的
onKeyUp()
function
onKeyUp() {
if (this.currentText.length >= 3) {
console.log("testing");
this.isSpinnerLoading = true;
this.menuContent = false;
setTimeout(() => {
this.menuContent = true;
}, 2000);
setTimeout(() => {
this.contentPlaceholder.openMenu();
}, 2001);
} else {
this.isSpinnerLoading = false;
}
}
Note that we are triggering openMenu()
after the this.menuContent()
becomes true请注意,我们在
this.menuContent()
变为 true 后触发openMenu()
Here is a link of stackblitz.这是stackblitz的链接。 It's not perfect but it can help.
它并不完美,但它可以提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.