【发布时间】:2020-10-26 14:08:39
【问题描述】:
您好,我在我的 angualr7 应用程序中使用角度材料自动完成组件,并且正在研究如何将 api 响应中的值绑定到它。任何人都可以提供解决此问题的解决方案吗?
HTML:
<mat-form-field>
<input type="text" [(ngModel)]="templateId" name="templateId" #templateId="ngModel" placeholder="Template" matInput
[matAutocomplete]="auto" aria-label="Number" (keyup)="filter($event)">
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onTemplateChange($event)">
<mat-option *ngFor="let temp of filtered" [value]="temp">
{{temp.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Ts:
onTripTemplateChange($event) {
this.templateId = $event.temp.name;
}
- 我需要在打开组件时默认选择值
- 当我从自动完成列表中更改选项时,我需要更改所选值
请告诉我灵魂..
【问题讨论】:
-
您应该阅读 excellent official docs 的 Angular Material 示例。
-
是的,已经完成了,但这些示例是响应式表单绑定,而不是模板驱动的表单..
标签: angular typescript angular-material angular7