angular - 如何隐藏/打开 Angular 材质下拉菜单
问题描述
我正在尝试隐藏/打开角度材料下拉菜单。
treps.html
<mat-form-field>
<mat-select #color formControlName="colors">
<mat-option value=''></mat-option>
<mat-option [value]="color" *ngFor="let color of colors"> {{color}}
</mat-option>
</mat-select>
<mat-form-field>
<button (click)="onClick()">Hide/Open</button>
treps.ts
@ViewChild('color') color
onClick() {
this.color.hide() // Error appears in the console,stating hide is not a function
}
我尝试检查所有属性,但无法隐藏下拉列表。
解决方案
您可能只是缺少要使用 ViewChild 获得的属性类型。尝试这个:
<mat-form-field>
<mat-select #color="matSelect" formControlName="colors">
<mat-option value=''></mat-option>
<mat-option [value]="color" *ngFor="let color of colors"> {{color}}
</mat-option>
</mat-select>
<mat-form-field>
<button (click)="onClick()">Hide/Open</button>
然后在你的控制器中:
@ViewChild('color') color: MatSelect;
onClick() {
this.color.hide() // Error appears in the console,stating hide is not a function
}
这里有一个例子。原谅糟糕的造型,我希望你从那里明白: https ://stackblitz.com/edit/angular-mkdakz?file=src%2Fapp%2Fapp.component.ts
推荐阅读
- javascript - How to stop my Hamburger menu from jumping me to the top of my page on click.
- swift - Swift conditional #if compilation fails in expression list
- html - button not disabled when disabled provided in fieldset
- python - 如何通过将光标放在框架内来调整框架的大小?
- performance - How to solve the system error when file is not found on specified location?
- node.js - Specifying draft order currency Shopify
- javascript - how to convert the JSON object into form data and send it as Data to web api fetch
- apache-camel - How to register route in Blueprint -
does not work - android - Retrofit Call GSON Return null Object
- macos - DarkMode 中的应用程序窗口在屏幕底部呈深红色