如何解决Angular 12 未绑定到来自 webapi 的值:返回 NgFor 仅支持绑定到可迭代对象,例如数组
我一直无法绑定到 Angular12/Typescript 中的 webapi 调用。
浏览器显示此错误: ERROR 错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到可迭代对象,例如数组。
已成功从 api 调用返回数据。我不确定我在这里缺少什么?
组件
@Component({
selector: 'app-system-of-record-search',templateUrl: './system-of-record-search.component.html',styleUrls: ['./system-of-record-search.component.scss']
})
export class SystemOfRecordSearchComponent implements OnInit {
model?: SystemOfRecord[];
constructor(
private configApi: ConfigApi
) { }
ngOnInit(): void {
this.configApi.searchSystemOfRecord().subscribe(
(data: SystemOfRecord[]) => {
this.model = data;
}
);
}
HTML
<div>
<div *ngIf="model">
<select id="selSystemOfRecord" class="form-control">
<option *ngFor="let item of model" [ngValue]="item.systemOfRecordId">{{item.name}}</option>
</select>
</div>
</div>
API 提供者:
searchSystemOfRecord(): Observable<SystemOfRecord[]> {
const url = `${this.domain}/api/systemofrecord/search`;
return this.http.post<SystemOfRecord[]>(url,{},{ responseType: 'json' });
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。