Angular/Typescript找不到不同的支持对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件
我用的是角的 试图调用api并从中检索数据,但它抛出了此错误Angular/Typescript找不到不同的支持对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件,angular,typescript,Angular,Typescript,我用的是角的 试图调用api并从中检索数据,但它抛出了此错误 core.js:4352 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. 我的服务看起来像 import { HttpClient } from '@angular/common/http';
core.js:4352 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
我的服务看起来像
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { User } from '../Models/user.model';
@Injectable({
providedIn: 'root'
})
export class DataService {
apiUrl = 'myurl is in here but i cannot show'
constructor(private _http: HttpClient) { }
getUsers(){
return this._http.get<User[]>(this.apiUrl);
}
}
export class User {
alertId: any;
itemId: string;
title: string;
description: string;
categoryId: any;
riskId: any;
sourceId: any;
startDate: any;
endDate: any;
link: string;
countryId: any;
countries: string;
keywordMatches: string;
keywordsMatched: 0;
createdDate: any;
createdBy: any;
isDeleted: true;
deletedDate: any;
deletedBy: any;
latitude: 0;
longitude: 0;
notes: string;
customerId: any;
formattedAddress: any;
firstname: any;
surname: any;
email: string;
phone: string;
smsEnabled: true;
customerName: string;
homeCountryId: any;
travellerTagId: 0;
}
这个t.s组件正在使用它
import { Component, OnInit } from '@angular/core';
import { User } from 'src/app/Models/user.model';
import { DataService } from 'src/app/Services/data.service';
@Component({
selector: 'app-side-nav-alerts',
templateUrl: './side-nav-alerts.component.html',
styleUrls: ['./side-nav-alerts.component.css']
})
export class SideNavAlertsComponent implements OnInit {
users$: User[];
constructor( private dataService : DataService) { }
ngOnInit(){
return this.dataService.getUsers()
.subscribe(data => this.users$ = data)
}
}
HTML
{{user.name}
有没有办法让我用这个循环一下?
我基本上只是想循环它并显示从api收集的信息。我知道可能需要将其转换为另一种数据类型,但我不确定如何进行转换。您应该尝试:
ngOnInit(){
this.users$ = this.dataService.getUsers()
}
而不是
ngOnInit(){
return this.dataService.getUsers()
.subscribe(data => this.users$ = data)
}
因为数据是实际数据,而不是订阅<代码>可观察的。
顺便说一句,你的ngOnInit
没有理由有返回语句
转向
进入
谢谢你的帮助!但我还是会犯这个错误类型“Observable”缺少类型“User[]”中的以下属性:json、length、pop、push和26 more.ts(2740)``这意味着您没有数组的可观察属性,只有其他属性。顺便说一句:您的用户类没有名为name的成员。stackblitz会很好。用户$:用户[];->用户$:可观察;您是否查看了浏览器开发工具中的响应?成功了吗?“数据”真的是一组用户吗?这个答案应该行得通。它的响应可能不是@acincognito所述的数组
ngOnInit(){
return this.dataService.getUsers()
.subscribe(data => this.users$ = data)
}
users$: User[];
users$: Observable<User[]>;
*ngFor = 'let user of (users$ | async)' style="text-align: center;"