简体   繁体   English

Angular2更新http请求中的值

[英]Angular2 update value from http request

I have a simple service does a few get requests: 我有一个简单的服务,它做了一些获取请求:

getDevices() {
  return this.http.get('https://api.particle.io/v1/devices')
    .map((response: Response) => response.json());
}

getVariable(deviceId: string) {
  return this.http.get('https://api.particle.io/v1/devices/' + deviceId + '/running')
    .map((response: Response) => response.json());
}

In my component I have the following function which fetches devices and adds the variable from getVariable to each device in this.devices. 在我的组件中,我具有以下函数,该函数获取设备并将getVariable中的变量添加到this.devices中的每个设备。

  getDevicesAndRunning() {
    function callback(array, data, res) {
      array.push(Object.assign(data, {"running": res.result}))
    }
    this.photonService.getDevices()
      .subscribe(
        data => {
          const myArray = [];
          for (var i=0; i<data.length; i++) {

            if (data[i].connected) {
              console.log(data, i);
              this.photonService.getVariable(data[i].id)
                .subscribe(
                  callback.bind(this, myArray, data[i])
                );
            };
          }
          this.devices = myArray;
        }
      );
  }

I can then call getDevicesAndRunning() to update the list of devices and iterate over them in the view. 然后,我可以调用getDevicesAndRunning()来更新设备列表并在视图中对其进行迭代。 Currently I'm doing this in OnInit and whenever the user clicks a button. 目前,我在OnInit以及用户单击按钮时都在执行此操作。 However, since this.devices gets repopulated each time the function is called the user sees a blank list for however long the request takes to respond. 然而,由于this.devices得到每次重新填充函数被调用用户将看到一个空白的列表但是长的要求需要做出回应。 All I want is for the res.result to be updated, not the entire array. 我想要的res.result要更新res.result ,而不是整个数组。

Since you are bulk updating the list you are blanking it out while the refresh is happening. 由于您要批量更新列表,因此在刷新时将其清空。 You will need to preserve the array and alter it on the fly. 您将需要保留阵列并即时更改它。 Splice out the removed items and push in the new ones. 拼接出已删除的项目并推入新的项目。 Let the sort function angular provides do the rest of the work. 让排序功能为您提供剩下的工作。

I do this sort of thing by not using the originally returned list. 我通过不使用原始返回的列表来执行此类操作。 I implemented something like this for an app that's in production now and it works great. 我为现在正在生产的应用程序实现了类似的功能,并且效果很好。

When you get the original list, stash it off to the side. 当您得到原始清单时,将其藏在一边。 Create a copy and use that for the display. 创建一个副本并将其用于显示。

When you need to update it, get the new list, copy it, and now you have a lot of options; 当您需要更新它时,获取新列表,将其复制,现在您有了很多选择。 old vs. new, replace old, combine old and new, display delta, etc. There's some nice ES6 functions, not native but simple enough, out there to do things like unions, diffs, etc, and of course lodash will serve. 有旧有新,有新有旧,新旧结合,显示增量,等等。ES6有一些不错的功能,不是本机而是足够简单的,可以执行诸如并集,差异之类的事情,当然lodash也可以使用。

True, this involves a copy of the list, but in the app I built I dealt with thousands of objects and at least six lists simultaneously and never had any problems with memory etc. You just have to remember to clean it up if necessary. 的确,这涉及到列表的副本,但是在我构建的应用程序中,我同时处理了数千个对象和至少六个列表,而内存等从来没有任何问题。您只需要记住在必要时清理它。

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

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