angular - 与按钮一起使用的 mat-autocomplete(如 md-autocomplete)
问题描述
在 Angular Material 1 中,可以通过单击按钮打开md-autocomplete
下拉列表(参见doc)。
mat-autocomplete
在 Angular Material 2 中,我认为(cf doc )没有这种可能性。这在某种程度上仍然可能吗?如何 ?我正在考虑使用input
隐藏和触发器openPanel
,但对于这样一个简单的用法来说似乎有点矫枉过正......
谢谢你的帮助
[编辑]
现在我的代码是这样的(我没有添加按钮,因为我不确定这是正确的方式)
<mat-form-field>
<input type="text" placeholder="Pronostique le futur vainqueur" aria-label="Vainqueur" matInput
[(ngModel)]="worldcupWinner" name="worldcupWinner" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let team of teams" [value]="team">
<img class="flag" [src]="team.flag_url" />
<span class="label">{{ team.name }}</span>
</mat-option>
</mat-autocomplete>
</mat-form-field>
解决方案
如果您添加代码,我将添加引用和函数调用。
编辑
<mat-form-field>
<input #trigger="matAutocompleteTrigger" type="text" placeholder="Pronostique le futur vainqueur" aria-label="Vainqueur" matInput [(ngModel)]="worldcupWinner" name="worldcupWinner" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let team of teams" [value]="team">
<img class="flag" [src]="team.flag_url" />
<span class="label">{{ team.name }}</span>
</mat-option>
</mat-autocomplete>
</mat-form-field>
<button mat-raised-button (click)="openThatPanel()">OPEN IT</button>
组件.ts:
@ViewChild('trigger') trigger: MatAutocompleteTrigger;
openThatPanel() {
setTimeout(_ => this.trigger.openPanel());
}
不幸的是,在 trigger.openPanel() 周围没有 setTimeout 的情况下,我无法打开它。
推荐阅读
- java - Java:侦听具有更多线程的单个端口与侦听多个端口
- ios - 组织太多数组 Xcode objc
- python - 由索引矩阵指定的 Numpy 选择矩阵,来自多维数组
- python - Keras对隐藏层定义的澄清
- java - 调用我的 Web 服务时出现 SSL 协议错误
- docker - Google Compute Engine App 没有监听端口 80 和 443
- javascript - 试图从数据属性中找到 id 并设置它
- javascript - 单击a时如何进行多个ajax调用
- 元素
- arrays - 在 bash 中使用递增/递减
- docker - 为什么“Docker run”每次都会创建新容器?