【发布时间】:2020-11-21 13:35:43
【问题描述】:
我正在使用角度 5 和材质 2。
在 ts 文件中,我有这个属性:
filteredOptions: Observable<any[]>;
此属性将有一组值显示在自动完成字段中。
[{
id:'1', name: 'teste 1'},
{id:'2', name: 'teste 2'},
{id:'3', name: 'teste 3'
}]
这个值数组来自数据库,它会在用户输入之后显示出来。
html 文件:
## <form class="example-form">
## <mat-form-field class="example-full-width">
## <input type="text" placeholder="Assignee" aria-label="Assignee" matInput [formControl]="myControl" [matAutocomplete]="auto">
## <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
## <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
## {{ option.name }}
## </mat-option>
## </mat-autocomplete>
## </mat-form-field>
## </form>
ts 文件示例:
this.filteredOptions = Observable<any[]>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith({}),
map(getArrayOfValue(val))
);
}
// it is going to call an api to get the array of values to be shown in the field
getArrayOfValue(val): Observable<any[]> {
const url = this.ROOT_URL + 'veiculo/' + val;
return this.http.get<any[]>(url);
}
这段代码给我一个错误
src/app/oficina/cadastro/veiculo/veiculo.component.ts(55,5) 中的错误: 错误 TS2322:类型 'Observable>' 不可分配 输入“可观察”。类型“可观察”不是 可分配给类型“任何[]”。 “Observable”类型中缺少属性“includes”
【问题讨论】:
-
您的传入数据是什么样的?请将 JSON 响应复制粘贴到您的问题。您正在谈论获取一个对象,但您在问题中显示的是一个数组?
-
感谢您的回答。我换个问题。我不确定我是否足够清楚,但我想要的是使用来自数据库而不是来自页面中的对象的数据加载自动完成。
标签: angular rest angular-material2