2017-04-04 43 views
0

我有一個空數組,名爲rows,我想用* ngFor來顯示它。在數組的每一行上,我傳遞來自http請求的響應。所有的http請求都被順序調用。即使數組輸入正確,* ngFor也不會更新。我希望在我收到它的請求響應時按順序顯示每一行。Angular 2 * ngfor不會使用連續的http請求響應進行更新

<div class="row" > 
    <my-rows 
     *ngFor="let row of rows | keys; trackBy: trackByFn; let index = index" 
     [month]='row.value' 
     [title]='row.key'> 
    </my-rows> 
</div> 
getRerports(url: string, dates: any, id: any){ 
    let params = new URLSearchParams() 
    params.set('company_id',id) 
    params.set('date',dates[0]) 
    this.authHttp.get(this._getReportsUrl,{search:params}) 
     .subscribe(
      data=>{ 
       let formatedDate = moment(data.json().date,'YYYY-MM-DD').format('MMMM YYYY') 
       this.rows[formatedDate] = data.json() 
       dates.splice(0,1) 
       this.flag = false 
       if(dates.length>0){ 
        this.getRerports(this._getReportsUrl,dates, id) 
       }else{ 
        this.flag=true 
       } 
      }, 
      err=> console.log(err) 
     ) 
} 

trackByFn(index:any, item:any) { 
    return index; 
} 
+0

更多代碼請。 – n00dl3

+0

請發佈您的http請求的詳細信息。 – Pengyy

+0

@ n00dl3我已經添加了http req –

回答

0

如果不更新使用ChangeDetectorRef視圖。

constructor(private ref: ChangeDetectorRef) 

更新數組時使用this.ref.detectChanges()。

+1

它似乎不起作用。我已經發布它在陣列更新下。 –

+0

您是否正確地獲取了數組中的值?如果它只是未更新的視圖,則應該起作用。 – RemyaJ

+0

Yeap數組已正確更新並且所有請求都已成功完成。當我在外部div中使用帶有* ngIf的標誌變量時,我一次獲得所有行。問題是我想要按順序打印它們。 –

0

我已經盡力改變我提供的對象行陣列,而不是

this.rows[formatedDate] = data.json() 

我已經使用這個:

let object = {key:formatedDate, value: data.json()} 
       this.rows.push(object) 

,它似乎工作得很好。 所以問題出在數組初始化。