【发布时间】:2018-04-02 22:33:00
【问题描述】:
我正在尝试让 Angular Material 2 自动完成来搜索 API,而不是按照示例在数组内搜索。这是我尝试过的:
HTML:
<mat-form-field fxFlex="33" class="mr-20 mt-20">
<input matInput placeholder="State" aria-label="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let state of filteredStates | async" [value]="state.name">
<span>{{ state.name }}</span>
</mat-option>
</mat-autocomplete>
</mat-form-field>
TS:
this.stateCtrl = new FormControl();
this.filteredStates = this.stateCtrl.valueChanges
.startWith(null)
.debounceTime(400)
.do(value => {
this.ClientsService.search(value).then( res => {
console.log(res, 'oi');
this.states = res;
})
});
但是,当我输入时,我收到此错误:Error: Cannot find a differ supporting object 'e' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
请注意,e 是我在搜索字段中输入的内容。我从服务器得到的响应是一个包含对象的数组。
我在这里错过了什么?
【问题讨论】:
-
你在
filteredStates得到值吗 -
您忘记将从服务器获取的数据解析为 JSON。做一个 JSON.parse() 它应该可以工作
-
@Timothy 我正在解析服务端的数据,所以不是这样。当我再次尝试解析数据时,我得到了同样的错误。
-
@RahulSingh 是的,我确实得到了一个包含对象的数组
标签: angular material-design angular-material2