【发布时间】:2019-07-08 06:10:00
【问题描述】:
我有一个简单的angular material autocomplete 函数。现在我想以编程方式设置此自动完成的值/选定选项。这是我的代码:
hardware.component.html
<tr>
<td class="desc pd7">Firmware</td>
<td>
<div class="form-group mb0">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Select firmware" aria-label="Number" matInput [formControl]="myControlFirmware" [matAutocomplete]="auto1" (keydown.enter)="$event.preventDefault()">
<mat-autocomplete #auto1="matAutocomplete" [displayWith]="displayFirmwares">
<mat-option *ngFor="let firmware of filteredFirmwares | async" [value]="firmware" (onSelectionChange)="getIdFromFirmware($event, firmware)">
{{ firmware.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
</td>
</tr>
hardware.component.ts
this.miscellaneousTerminalService.getMiscellaneous('firmware').subscribe(data => {
if(data.success) {
this.availableFirmwares = data.data;
this.filteredFirmwares = this.myControlFirmware.valueChanges
.pipe(
startWith(''),
map(valFirmware => this.filterFirmwares(valFirmware))
);
}
});
filterFirmwares(valFirmware: any): any[] {
return this.availableFirmwares.filter(firmware => {
let name = valFirmware.name ? valFirmware.name : valFirmware;
return firmware.name.toLowerCase().indexOf(name.toLowerCase()) > -1;
});
}
displayFirmwares(firmware: any): string {
return firmware ? firmware.name : firmware;
}
getIdFromFirmware(event, firmware) {
if(event.isUserInput) {
this.selectedFirmware = firmware._id;
}
}
我尝试将我的固件型号设置为所选固件的_id 或名称,但没有任何效果。如何以编程方式设置值。我正在使用 Angular 材质 @V 7
【问题讨论】:
-
所以基本上你想将
input的值设置为一个特定的选项?像渲染组件时的默认值一样?