【发布时间】:2018-09-14 03:46:39
【问题描述】:
我正在尝试使用类似于以下示例的mat-autocomplete 实现过滤器;
所以我正在尝试实现该功能,以便当用户开始输入交易时,他们正在寻找基于字符串中任何位置的部分字符串匹配的过滤器,并在选项中突出显示。
我目前在我的 .html 文件中
<mat-form-field class="form-group special-input">
<input type="text" placeholder="Select a trade" aria-label="Select a trade" matInput [formControl]="categoriesCtrl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" md-menu-class="autocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.name">
{{ option.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
我的 .ts 在哪里
categoriesCtrl: FormControl;
filteredOptions: Observable<ICategory[]>;
options: ICategory[];
categorySubscription: Subscription;
constructor(fb: FormBuilder, private router: Router, private service: SearchService, private http: Http) {
this.categoriesCtrl = new FormControl();
}
ngOnInit() {
this.categorySubscription = this.service.getCategories().subscribe((categories: ICategory[]) => {
this.options = categories;
this.filteredOptions = this.categoriesCtrl.valueChanges
.pipe(
startWith(''),
map(options => options ? this.filter(options) : this.options.slice())
);
});
}
ngOnDestroy() {
this.categorySubscription.unsubscribe();
}
filter(val: string): ICategory[] {
return this.options.filter(x =>
x.name.toUpperCase().indexOf(val.toUpperCase()) !== -1);
}
ICategory 是一个基本接口。
export interface ICategory {
value: number;
name: string;
}
而服务 getCategories() 只是从 api 返回所有类别。
代码当前正在运行并按照此示例构建;
Angular Material mat-autocomplete example
我想在选项字符串中添加突出显示术语的效果?这可能吗?
【问题讨论】:
标签: angular typescript angular-material