【发布时间】:2019-03-11 09:49:05
【问题描述】:
我的要求是在输入框中显示选定的值,并将选定的 Id 和值获取到 .ts 文件中。由于我需要 Id 和 Value,我将选项值直接绑定到 [value]。但如果我这样做,它就会被打印为 [Object Object]。
<mat-form-field appearance="outline" class="w-100">
<mat-label>Enter Hotel Name</mat-label>
<input type="text" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selectedclient($event)">
<mat-option *ngFor="let option of clients; let i = index" [value]="option">
{{ option.name }}
</mat-option>
</mat-autocomplete>
<mat-icon matSuffix>location_on</mat-icon>
</mat-form-field>
Ts 文件
options = [
{ id: 1, name: 'One'},
{ id: 2, name: 'Two'},
{ id: 3, name: 'Three'}
];
selectedclient(event) {
console.log(event.option.value);
}
Stackblitz 编辑器网址: https://stackblitz.com/edit/angular-mat-select-data-n4tvmj
【问题讨论】:
标签: angular angular-material-6