2017-04-26 35 views
0

數據表一旦呈現後,我無法更新數據。 我正在使用angular2-datatable。angular2-datatable無法更新數據

在我appcomponent.html:

如果我更新數據2在我appcomponent.ts文件中像這樣:

this.httpservice.getdata().subscribe 
(data=> 
{ this.data2=data; 
    for (i = 0; i < 21; i++) { 
    markerarray[i] = new google.maps.Marker({ 
    position: new google.maps.LatLng(this.data2[i].latituide, this.data2 
    [i].longituide),   //In this line 
    title: this.data2[i].name, //and this 
    map: map 
    }); 
     } 
    google.maps.event.addListener(map, 'idle', function() { 

      var bounds = map.getBounds(); 
      for (var i = 0; i < 20; i++) { 
      var marker = markerarray[i]; 
      if (bounds.contains(marker.getPosition()) === true) { 
      this.data2.length=0;// This is not working for me 
      } 
      } 

     }); 
}) 

我要清空數據表,一旦指標有界。

+0

嘗試做this.data2 = [] –

+0

使用'箭頭function'保留'this',而不是'{功能' – yurzui

+0

@JuliaPassynkova我嘗試這樣做。但它仍然沒有更新:( – mistletoe

回答

0

我想出了一個解決方案,但我不確定這是最好的方法。

我已經使用ChangeDetectorRef來檢測變化。

constructor(private http: Http,private httpservice: HttpService,private ref: ChangeDetectorRef) { 
     ref.detach(); 
    setInterval(() => { 
     this.ref.detectChanges(); 
    },1000); 
    } 
+0

它可能會傷害你的性能應用程序每秒都會執行detectChanges,並且您還需要在OnDesctroy中刪除間隔 - 否則會導致內存泄漏。 –

+0

@JuliaPassynkova哦,好吧。您有任何其他解決方案嗎? – mistletoe

+0

我使用數據表並且我可以修改推送更改如果您認爲您有更改檢測問題,請嘗試執行「setTimeout(()=> { window.dispatchEvent(new Event('resize')); });「在您更改data2之後。 –