所以我遵循Angular文件中的Angular Material Autocomplete,但是已经花了两天时间从自动完成中获取选择的值。基本上我想要的是自动完成显示人类'名称和姓氏,但选项的值必须是整个Human对象。然后,我只想在选择Human时每次都调试Selected.uman。任何解决方案都可能会这样做。
以下是一个可以使用的演示项目:https://stackblitz.com/edit/angular-hnu6uj
这是html文件:
<input [(ngModel)]="SelectedHuman" (change)="OnHumanSelected()" matInput [formControl]="MyControl" [matAutocomplete]="auto" placeholder="Human">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="AutoCompleteDisplay">
<mat-option *ngFor="let human of arrFilteredHumans | async" [value]="human">
{{human.Name}} - {{human.Surname}}
</mat-option>
</mat-autocomplete>
这是ts文件: 导出类AutocompleteDisplayExample实现OnInit {
SelectedHuman: Human;
MyControl = new FormControl();
arrFilteredHumans: Observable<Human[]>;
arrHumans = [
new Human('1K59DN3', 27, 'John', 'Smith'),
new Human('9VH23JS', 67, 'William', 'Shakespeare'),
new Human('0QNF1HJ', 44, 'Elon', 'Musk')
];
ngOnInit() {
this.arrFilteredHumans = this.MyControl.valueChanges.pipe(
startWith(''),
map((val) => this.filter(val))
);
}
filter(val: any): Human[] {
return this.arrHumans.filter((item: any) => {
//If the user selects an option, the value becomes a Human object,
//therefore we need to reset the val for the filter because an
//object cannot be used in this toLowerCase filter
if (typeof val === 'object') { val = "" };
const TempString = item.Name + ' - ' + item.Surname;
return TempString.toLowerCase().includes(val.toLowerCase());
});
}
AutoCompleteDisplay(item: any): string {
if (item == undefined) { return }
return item.Name + ' - ' + item.Surname;
}
OnHumanSelected() {
console.log(this.MyControl); //This has the correct data
console.log(this.MyControl.value); //Why is this different than the above result?
console.log(this.SelectedHuman); //I want this to log the Selected Human Object
}
}
export class Human {
constructor(
public ID: string,
public Age: number,
public Name: string,
public Surname: string
) { }
}
答案 0 :(得分:2)
下面是您可以查看的stackBitz链接
以下是代码
OnHumanSelected(SelectedHuman) {
console.log(SelectedHuman); // get from view
console.log(this.SelectedHuman); // get from variable
}
HTML文件
<input [(ngModel)]="SelectedHuman" matInput [formControl]="MyControl" [matAutocomplete]="auto" placeholder="Human">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="AutoCompleteDisplay">
<mat-option (click)="OnHumanSelected(SelectedHuman)" *ngFor="let human of arrFilteredHumans | async" [value]="human">
{{human.Name}} - {{human.Surname}}
</mat-option>
</mat-autocomplete>
由于
答案 1 :(得分:2)
点击事件和选择事件是两个不同的事件。您想要所选的事件,而不是点击事件,因此请使用optionSelected
中的MatAutocomplete
:
<mat-autocomplete
#auto="matAutocomplete"
[displayWith]="AutoCompleteDisplay"
(optionSelected)="OnHumanSelected($event.option)">
<!--
...
-->
</mat-autocomplete>
OnHumanSelected(option: MatOption) {
console.log(option.value);
}
答案 2 :(得分:1)
所以这似乎解决了这个问题。将OnHumanSelected()函数放在mat-option的(click)事件中:
<mat-option (click)="OnHumanSelected()" *ngFor="let human of arrFilteredHumans | async" [value]="human">
{{human.Name}} - {{human.Surname}}
</mat-option>
####################
OnHumanSelected() {
console.log(this.SelectedHuman);
}