鑑於

2017-01-03 66 views
1

在我的項目「無限滾動日曆」離子2 ngFor更新數據我用ngFor到interate超過對象使用「管」 mapToIterable。鑑於

不幸的是,我注意到,當我添加新的屬性,對象上的視圖變量被更新,但不ngFor更新列表。 的事情是,當我進入查看它填補了罰款,但是當我向下滾動它成功地從API獲取數據,但不能更新ngFor列表..

我放置代碼zone.run()沒有成功嘗試解決方法的問題。

#ionic info 

Your system information: 

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.3 
Ionic CLI Version: 2.1.18 
Ionic App Lib Version: 2.1.9 
Ionic App Scripts Version: 0.0.45 
ios-deploy version: Not installed 
ios-sim version: Not installed 
OS: Linux 4.4 
Node Version: v6.5.0 
Xcode version: Not installed 

時間的一些代碼:

// mapToIterable.pipe.ts 
import { Pipe, PipeTransform } from '@angular/core'; 

@Pipe({name: 'mapToIterable'}) 
export class MapToIterablePipe implements PipeTransform { 
    transform(value): any { 
    let keys = []; 
    for (let key in value) { 
     keys.push({ key: key, value: value[key]}); 
    } 
    return keys; 
    } 
} 

// index.ts 
export class Appointments { 
    appointments = {}; 
    current_date = new Date(); 
    top_date: any = new Date(+new Date - 12096e5); // 14 days past 
    bottom_date: any = new Date(+new Date + 12096e5); // 14 day future 
    range = { 
    start_date: this.top_date, 
    end_date: this.bottom_date 
    } 

    generateDays = function (start, end) { 
    while(start < end) { 
     start.setDate(start.getDate() + 1); 
     this.appointments[start.toISOString().slice(0, 10)] = []; 
    } 
    }; 

    fillDays = function() { 
    this.appointmentService.all(this.range).map(res => res.json()).subscribe(
     data => { 
     for (var appointment in data) { 
      this.appointments[appointment] = data[appointment]; 
     } 
     }, 
     err => { 
     console.log(JSON.parse(err._body)); 
     } 
    ); 
    }; 

    constructor(
    public navCtrl: Nav, 
    public appointmentService: AppointmentService, 
) { 
    var temp_date: any = new Date(this.top_date); 
    this.generateDays(temp_date, this.bottom_date); 
    this.fillDays(); 
    } 

    moreFuture(infiniteScroll) { 
    setTimeout(() => { 
     var temp_date: any = new Date(this.bottom_date); 
     this.bottom_date = new Date(+temp_date + 12096e5); // add 14 days 
     this.range.start_date = temp_date; 
     this.range.end_date = this.bottom_date; 
     this.generateDays(temp_date, this.bottom_date); 
     this.fillDays(); 
     infiniteScroll.complete(); 
    }, 500); 
    }; 
} 

。我想用對象而不是數組?

,因爲你使用的是「純管道」的當值或引用它適用於改變執行我將每一個答案:)

回答

2

這是感激。 它不會檢測對象屬性changes.In你的情況下,mapToIterable管是不知道的變化存在的對你的對象的屬性Check here

嘗試

@Pipe({name: 'mapToIterable',pure:false}) 
+0

謝謝:) 我已經看到了之前,但我沒想到它是如此重要:/ 我試圖修改代碼以使用可觀察和解決方案是如此簡單... – m1l05z

+1

我經歷過同樣的事情。發現它很難:) –