要解决Angular 7 mat自动完成与过滤器不起作用的问题,你可以尝试以下几个步骤:
MatAutocompleteModule
和MatInputModule
模块。import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatInputModule } from '@angular/material/input';
{{option}}
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
@Component({
selector: 'app-autocomplete',
templateUrl: './autocomplete.component.html',
styleUrls: ['./autocomplete.component.css']
})
export class AutocompleteComponent {
myControl = new FormControl();
options: string[] = ['Option 1', 'Option 2', 'Option 3'];
filteredOptions: Observable;
constructor() {
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(''),
map(value => this._filter(value))
);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.options.filter(option => option.toLowerCase().includes(filterValue));
}
}
这些步骤应该能够解决Angular 7 mat自动完成与过滤器不起作用的问题。确保你遵循了这些步骤,并适当地修改代码以适应你的需求。