【发布时间】:2019-03-08 07:16:56
【问题描述】:
我正在尝试将一些字符串从 json 响应转换为数字。例如
邮政编码:“92998-3874”至 92998-3874。遇到了这个SO。但不要在失败的地方。然后我需要在我的视图中循环它,但在堆栈闪电战控制台中面临以下错误。
ERROR
Error: Cannot find a differ supporting object '[object Object]' of type 'Clementina DuBuque'. NgFor only supports binding to Iterables such as Arrays.
Unable to display error. Open your browser's console to view.
到目前为止:
export class AppComponent {
name = 'Angular';
private _userList = [];
private _userListAddress:any = [];
_sampleURL = 'https://jsonplaceholder.typicode.com/users';
constructor(private _http: HttpClient) { }
_loadData() {
this._http.get<any[]>(this._sampleURL).subscribe(data => {
this._userList = data;
this._userListAddress = data[0].address.suite;
console.log(this._userList);
for (var _address in this._userList) {
this._userListAddress = this._userList[_address];
console.log('address',
parseInt(this._userListAddress.address.zipcode));
/*
//below snippet is used to convert strings to num
[].forEach.call(this._userList, function (a, b) {
[].forEach.call(Object.keys(a), function (c) {
if (!isNaN(this._userList[b][c]))
this._userList[b][c] = +this._userList[b][c];
});
});
console.log(this._userList); */
}
}
)
}
}
<button (click)="_loadData()">Load data</button>
<div *ngFor="let list of _userList">
<ul>
<li>{{list.address.zipcode}}</li>
</ul>
<div *ngFor="let addresList of _userListAddress">
<ul>
<li>{{addresList.address.zipcode}}</li>
</ul>
</div>
</div>
哪里做错了或建议更好的方法来实现这一点。 非常感谢。
【问题讨论】:
-
能把你做过的stackblitz的链接制作出来吗?
-
@ManirajfromKarur 我已经更新请检查链接
-
1) 为什么要将字符串转换为数字? 2)你是否意识到这会导致表达式和结果将是一个减法
-
我必须开始一些其他要求。为此,我需要将其从字符串转换为数字,然后只有我才能得出其他待定要求是否可行的结论。这就是为什么。我必须在我的路线图中实施 contentEditable 概念
标签: json angular loops angular6