【发布时间】:2020-01-11 03:21:37
【问题描述】:
我尝试从 api 使用自动完成,但它不起作用。 它只在没有 api 的情况下工作。
这是我的组件 TS:里面,有一个回调方法与来自 api 的数据(onGetTaxList)
import { Component, OnInit } from '@angular/core';
import { UsersService } from '../../../../core/services/users.service';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
@Component({
selector: 'app-create-process-modal',
templateUrl: './create-process-modal.component.html',
styleUrls: ['./create-process-modal.component.sass']
})
export class CreateProcessComponent implements OnInit {
myControl = new FormControl();
options = [
{ name: 'One' },
{ name: 'Two' },
{ name: 'Tree' },
];
filteredOptions: Observable<any>;
constructor(private service: UsersService) { }
ngOnInit() {
this.service.createNewProcess((data) => this.onGetTaxList(data));
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
}
onGetTaxList(data) {
console.log(data);
}
private _filter(value: string) {
const filterValue = value.toLowerCase();
return this.options.filter(option => option.name.toLowerCase().includes(filterValue));
}
}
组件html:
<div class="formContainer">
<h2 style="text-align: right">New Process</h2>
<mat-form-field style="text-align: right">
<input type="text" placeholder="Start Typing..." matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.name">
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
在这种状态下,它与对象一起工作
options = [
{ name: 'One' },
{ name: 'Two' },
{ name: 'Tree' },
];
现在我想从数据 api 中获得它的工作:
0: {companyName: "ziro", cid: "524023240", partners: Array(4)}
1: {companyName: "plus", cid: "524023240", partners: Array(2)}
我需要自动完成过滤公司名称。 谢谢。
【问题讨论】:
标签: angular autocomplete angular-material