Angular material 自动完成没有触发 onChange 事件?
Angular material autocomplete not firing off onChange event?
我有一个基本的自动完成功能。一个对象列表,我在其中拉出一个字符串 属性 的列表并希望对其进行过滤和 select 。知道为什么一切正常,我看到了名单。但是当我在框中键入时,过滤器不会发生?
<mat-form-field>
<input type="text" placeholder="Filter by Last Name" [formControl]="fcOnBehalfLastName" matInput
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let eln of allEmployeesLastNames" [value]="eln">
{{eln}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
fcOnBehalfLastName = new FormControl();
filteredLastNames: Observable<any>;
allEmployeesLastNames: string[];
constructor(private fb: FormBuilder) {
console.log('-----OrderStep1xComponent');
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.allEmployeesLastNames.filter(eln => eln.toLowerCase().includes(filterValue));
}
ngOnChanges(changes: SimpleChanges) {
if (changes.allEmployees && changes.allEmployees.currentValue) {
this.allEmployeesLastNames = this.allEmployees.map(o => o.lastName);
}
}
ngOnInit(): void {
this.employeeFilter = new Employee();
this.filteredLastNames = this.fcOnBehalfLastName.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
Aakash Verma 在另一个问题中有很好的答案:
使用输入上的 (change)
事件来检测变化。
我有一个基本的自动完成功能。一个对象列表,我在其中拉出一个字符串 属性 的列表并希望对其进行过滤和 select 。知道为什么一切正常,我看到了名单。但是当我在框中键入时,过滤器不会发生?
<mat-form-field>
<input type="text" placeholder="Filter by Last Name" [formControl]="fcOnBehalfLastName" matInput
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let eln of allEmployeesLastNames" [value]="eln">
{{eln}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
fcOnBehalfLastName = new FormControl();
filteredLastNames: Observable<any>;
allEmployeesLastNames: string[];
constructor(private fb: FormBuilder) {
console.log('-----OrderStep1xComponent');
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.allEmployeesLastNames.filter(eln => eln.toLowerCase().includes(filterValue));
}
ngOnChanges(changes: SimpleChanges) {
if (changes.allEmployees && changes.allEmployees.currentValue) {
this.allEmployeesLastNames = this.allEmployees.map(o => o.lastName);
}
}
ngOnInit(): void {
this.employeeFilter = new Employee();
this.filteredLastNames = this.fcOnBehalfLastName.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
Aakash Verma 在另一个问题中有很好的答案:
使用输入上的 (change)
事件来检测变化。