2016-07-24 82 views
1

我不知道如何通過我的對象對象(但它應該是數組): Data structureAngular2 rc1 ngFor「無法找到類型爲'object'的不同支持對象'[object Object]'

Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

視圖結構:

<a *ngFor="let msg of messages " href class="clearfix"> 
      <div class="img-area"><img [ngClass]="{'photo-msg-item': !msg.from}" 
            src="{{ (msg.from || (msg.from | baProfilePicture)) }}"></div> 
      <div class="msg-area"> 
      <div>{{ msg.message }}</div> 
      <span>{{ msg.send_date }}</span> 
      </div> 
     </a> 

組件:

public notifications:Array<Object>; 
    public messages:Array<Message>; 
    public data_error: Boolean = false; 
    constructor(private _baMsgCenterService:BaMsgCenterService) { 
     this.messages = this._baMsgCenterService.getMessages().subscribe(
     (messagess) => { 

      let result:Array<Message> = []; 
      if (messagess) { 
      messagess.forEach((message) => { 
      result.push(
       new Message(message[0], 
         message[1], 
         message[2], 
         message[3], 
         message[4]) 
       ); 
      }); 
     } 
     this.messages = result; 
     }, 
     err => { this.data_error = true} 
    ); 
+0

你可以發佈你的數據是可迭代的,也可以在那裏得到錯誤行號。 ?如果你提供代碼的話,效果會更好。 –

回答

1

第I你的問題所在,在這條線的油墨:

this.messages = this._baMsgCenterService.getMessages().subscribe(

看來你要Subscription對象設爲您的messages變量(subscribe方法返回Subscription對象https://github.com/ReactiveX/rxjs/blob/master/src/Observable.ts#L87

所以,你必須刪除此方影響。它應該是:

this._baMsgCenterService.getMessages().subscribe(
+0

很多,非常感謝。它工作,我愚蠢的錯誤。 –

+0

不客氣! – yurzui

0
<a *ngFor="let msg of messages | async " href class="clearfix"> 

嘗試加入一些 「異步」

here

相關問題