繁体   English   中英

如何在不刷新页面的情况下自动更新离子列表上的数据

[英]How to auto update data on ionic list without refreshing the page

我创建了一个包含 ionic 列表的页面,如果单击该按钮,它会调用 firebase api 并且它会得到更新,则有两个按钮。 它更新的,整个列表得到刷新。 如何停止刷新整个列表。

在此处输入图片说明

 onLocationDataUpdate() {
    // Get current location
    this.geolocation.getCurrentPosition().then(position => {
      this.location.lat = position.coords.latitude;
      this.location.lng = position.coords.longitude;

      // Get location data from Firebase
      this.connectivityServiceService.getLocations(
        this.locatingRange,
        this.location.lat,
        this.location.lng
      );

      // Bind data to Ionic list
      this.dataSetSubscription = this.connectivityServiceService.geoPointLocationUpdateNotify.subscribe(
        locations => {
          const locationData = this.connectivityServiceService.getGeoPointLocations();
          if (locationData.length > this.maxDataCount) {
            const tempLocationData = locationData.slice(0, this.maxDataCount);
            tempLocationData.forEach(item => {
              if (item.feed_back === undefined) {
                item = Object.assign(item, {
                  feed_back: { count: 0, positive: [], negative: [] }
                });
              }
            });
            this.geoPointLocationData = tempLocationData;
            this.loader.dismiss();
          } else {
            const tempLocationData = locationData;
            tempLocationData.forEach(item => {
              if (item.feed_back === undefined) {
                item = Object.assign(item, {
                  feed_back: { count: 0, positive: [], negative: [] }
                });
              }
            });
            this.geoPointLocationData = tempLocationData;
            this.loader.dismiss();
          }
        }
      );
    });
  }

我知道您已经满意地解决了这个问题,但您确实在这里遇到了一些问题。

只需将新数据推送到模型,您就会看到它更新,但在幕后,它正在重新创建整个列表。

如果数据变长,那么这可能会给您带来性能问题。 此时,您应该查看trackBy

这是一种给 Angular 一种方法来跟踪列表中每个单独项目的方法,以便在更新它时,Angular 可以确定哪些实际需要更新并且只更改它们。

这似乎是对该主题的一个不错的介绍

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM