【问题标题】:Angular2 Cannot find a differ supporting object of type 'string'. NgFor only supports binding to Iterables such as ArraysAngular2 找不到“字符串”类型的不同支持对象。 NgFor 仅支持绑定到 Iterables,例如 Arrays
【发布时间】:2017-07-29 21:06:56
【问题描述】:

我正在尝试从另一个视图获取数据,如下所示:

    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

我一直在阅读有关该错误的信息,看起来我不需要解析,所以我很困惑。

帮助,谢谢!

更新

这是转到另一个视图之前的数据。

【问题讨论】:

  • 您的categories 不是数组。它是一个对象。
  • 你能分享你正在传递的数据吗? PL。提出问题,不要发表评论
  • @Smit 在那里
  • 你可以先对你的对象进行字符串化,然后发送给参数。然后一旦你收到它......解析它。

标签: json angular angular2-routing


【解决方案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格式了。它会为你解析。[自动] :)

更多分析阅读:Angular 2: Passing Data to Routes?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2020-08-29
    • 2018-07-14
    • 2017-02-10
    相关资源
    最近更新 更多