2017-03-09 70 views
0

我試圖從另一種觀點認爲獲取數據,如: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

這是前往其他視圖之前的數據。

enter image description here

+0

你'categories'不是一個數組。它是一個對象。 – Smit

+0

你能分享你傳遞的數據嗎? PL。把這個問題不在評論 – Smit

+0

@Smit那裏是 –

回答

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?

相關問題