我正试图从另一个视图中获取数据,如下所示:
this.paramsSub = this.activatedRoute.params.subscribe(params => this.categories = params['obj']);
this.paramsSub = this.activatedRoute.params.subscribe(params => this.userInfo = params['usr']);
以这种方式发送数据:
this.router.navigate(['/path/obj', {obj: this.categories, usr: this.userInfo}]);
我正试图在html中显示带有* ngFor的类别:
<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
我一直在读这个错误,看起来我不需要解析,所以我很困惑。
帮助,谢谢!
更新
这是转到另一个视图之前的数据。
答案 0 :(得分:1)
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格式。它会为你解析。[自动]:)