Angular2无法找到'string'类型的不同支持对象。 NgFor只支持绑定到Iterables如数组
问题描述:
我试图从另一种观点认为获取数据,如:Angular2无法找到'string'类型的不同支持对象。 NgFor只支持绑定到Iterables如数组
this.paramsSub = this.activatedRoute.params.subscribe(params => this.categories = params['obj']);
this.paramsSub = this.activatedRoute.params.subscribe(params => this.userInfo = params['usr']);
数据以这种方式sended:
this.router.navigate(['/path/obj', {obj: this.categories, usr: this.userInfo}]);
我试图显示与* ngFor类别的HTML这样的:
<div *ngFor="let category of categories">
<input type="checkbox" value={{category.id}}/> {{category.name}}
</div>
我的错误:
Cannot find a differ supporting object '[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
我去另一种观点认为不错,但IDK为什么我的错误,如果我尝试:JSON.parse错误变化
Unexpected token o in JSON at position 1
Error: Error in :0:0 caused by: Unexpected token o in JSON at position 1
我一直在阅读有关该错误,它的样子我不” t需要解析,所以我非常困惑。
帮助,谢谢!
UPDATE
这是前往其他视图之前的数据。
答
this.router.navigate(['/path/obj', {obj: JSON.stringfy(this.categories), usr: JSON.stringfy(this.userInfo)}]);
这意味着现在你可以没有任何后顾之忧的对象! :)
this.paramsSub = this.activatedRoute.params.subscribe(params => this.categories = JSON.parse(params['obj']));
this.paramsSub = this.activatedRoute.params.subscribe(params => this.userInfo = JSON.parse(params['usr']));
你可以选择做解析,它不是必需的,因为,它已经在JSON格式。它会分析你[自动] :)
进行详细分析和阅读:Angular 2: Passing Data to Routes?
你'categories'不是一个数组。它是一个对象。 – Smit
你能分享你传递的数据吗? PL。把这个问题不在评论 – Smit
@Smit那里是 –