Javascript 如何在其已完成角度数据提取时逐个显示数据

Javascript 如何在其已完成角度数据提取时逐个显示数据,javascript,angular,typescript,Javascript,Angular,Typescript,HTML TS 这里的问题是,当它加载时,它将在显示所有数据之前获取所有数据。 我想在这里做的是,当第一个已经获取的项目出现时,它将显示,而其他项目仍在获取数据 例如,有5个项目,分别是区域1、区域2、区域3、区域4和区域5 当区域1已经完成提取数据时,它将显示该数据。而另一个仍在加载/获取 像区域1已经完成抓取,然后是区域3,然后是区域2,然后是区域5,然后是区域4 完成提取的用户将自动显示。尝试添加更改检测策略 并在使用第一个数据点更新变量后立即运行detectChanges函数。我将采用以

HTML

TS

这里的问题是,当它加载时,它将在显示所有数据之前获取所有数据。 我想在这里做的是,当第一个已经获取的项目出现时,它将显示,而其他项目仍在获取数据

例如,有5个项目,分别是区域1、区域2、区域3、区域4和区域5

当区域1已经完成提取数据时,它将显示该数据。而另一个仍在加载/获取

像区域1已经完成抓取,然后是区域3,然后是区域2,然后是区域5,然后是区域4

完成提取的用户将自动显示。

尝试添加更改检测策略
并在使用第一个数据点更新变量后立即运行detectChanges函数。我将采用以下两种方法之一来实现这一点。两者都不涉及承诺

我不打算尝试使用您的代码示例,因为它太大了,相反,我将关注您问题的核心——运行一系列可观察对象,并在返回结果时处理结果

一,。合并观测值

RxJS merge函数将同时运行多个observates,并立即使用单个值调用subscribe回调

可观测常数:可观测

演示设置了5个可观察对象,每个具有不同的延迟。在返回结果时处理观察值,并在完成时记录


正如您所看到的,它们在功能上是等价的。

我可以问一下,为什么要在承诺中包装可观察到的东西吗?您正在失去RxJS的许多简单性-包括您现在需要的功能。此外,您还向您的问题添加了许多代码。是否要在响应到达时处理对此.global.getData的响应?@KurtHamilton是否要在响应到达时处理对此.global.getData的响应?-是的,先生。我如何应用forkJoin或merge可观测值?因为我使用的是两个api,长官。我的例子处理一些任意的观察数组,不管它们来自哪里。我保持了我的示例的简单性,以便它可以应用于使用可观察对象的任何情况。例如,您可以执行forkJoin[service1.makeCall,service2.makeCall,service3.makeCall]。它们只是您一起运行的可观察对象。如何修复此类型“Promise”缺少类型“Observable”的以下属性。请像常量observables那样尝试:Observable[]=promises;
<div nz-row *ngIf="tempThermometer | async as temp">
<div *ngFor="let data of temp;let i = index;" nz-col nzXs="24" nzSm="12" nzMd="12" nzXl="8" nzXXl="6">
<nz-spin nzTip="Loading..." [nzSize]="'large'" [nzSpinning]="data.spinning">
                            <div echarts [options]="chartOption[i]" [autoResize]="true" style="height: 270px;"></div>
                          </nz-spin>
    </div>
    </div>
 tempLoading = false;
  tempThermometer = new BehaviorSubject<any>([]);

getRoomList() {
    this.tempLoading = true;
    this.subscription = this.global
      .getData(`/conditions/latest?length=${this.pageSize}`)
      .pipe(take(1))
      .subscribe((res: any) => {
        this.tempThermometer.next(Object.values(res['data'].map((obj: any) => {
          return {
            ...obj,
            spinning: true
          };
        })));

        this.tempLoading = false;
        this.lineChart(this.tempThermometer.value);
      });
  }
lineChart(params?: any) {
    const _this = this;
    const list: any = [];

    params.forEach((param: any) => {
      const url = encodeURIComponent(param.sensor);
      // List URL
      list.push(`/conditions?length=${this.length}&sensor=${url}`);
    });
    // Promise requests
    const promises = list.map(
      (url: any) =>
        new Promise(resolve => {
        this.subscription =  this.global.getData(url).pipe(take(1)).subscribe((res) => {
            resolve(res);
          }, (err: Error) => {
            return reject(err);
          });
        })
    );
    // Retrieve each data as per promise
    Promise.all(promises).then(results => {
      const dataRoom: any = [];

      results.map((result) => {
        const date: any = [], temperature: any = [], humidity: any = [], newRoomData: any = [];
        const param = result['data'];
        const roomData = orderBy(param, ['date'], ['asc']);
        const room = roomData.slice(-1)[0];
        const timeEnd = room.date.slice(0, 19);
        const timeStart = subHours(timeEnd, 7);
        const dataHour = roomData.filter((data: TemplogRecord) => {
          return !isBefore(data.date, timeStart) && !isAfter(data.date, timeEnd);
        });

        // console.log(roomData);

        const hash = Object.create(null);

        dataHour.forEach((data: any) => {
          const key = data.date.slice(0, 13);

          if (!hash[key]) {
            hash[key] = {
              sensor: data.sensor, temperature: data.temperature,
              humidity: data.humidity, date: key + ':00:00'
            };
            newRoomData.push(hash[key]);
          }
        });

        for (let x = 0; x < newRoomData.length; x++) {
          temperature.push(newRoomData[x].temperature);
          humidity.push(newRoomData[x].humidity);
          date.push(newRoomData[x].date);
        }

        dataRoom.push({
          date: date,
          humidity: humidity,
          temperature: temperature
        });
      });

      dataRoom.forEach((param: any, index: number) => {
        const option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              animation: false
            },
            backgroundColor: 'rgba(245, 245, 245, 0.8)',
            borderWidth: 1,
            borderColor: '#ccc',
            padding: 10,
            textStyle: {
              color: '#000'
            },
            formatter: function (prm: any) {
              let rec = prm[0].name.slice(0, 10) + '<br/>' + prm[0].name.slice(11, 19) + '<br/>';

              for (let x = 0; x < prm.length; x++) {
                if (prm[x].axisIndex !== 1) {
                  rec += prm[x].marker + ' ' + prm[x].seriesName + ': '
                    + prm[x].data + _this.units['Celcius'] + '<br/>';
                } else {
                  rec += prm[x].marker + ' ' + prm[x].seriesName + ': '
                    + prm[x].data + '%' + '<br/>';
                }
              }
              return rec;
            }
          },
          ...this.echart.roomChart,
          dataZoom: [{
            type: 'inside',
            show: false,
            bottom: 0,
            width: '84%',
            xAxisIndex: [0, 1],
            zoomOnMouseWheel: false,
          },
          {
            type: 'slider',
            bottom: 0,
            show: false,
            width: '84%',
            xAxisIndex: [0, 1],
            zoomLock: false,
          }],
          xAxis: [{
            type: 'category',
            boundaryGap: false,
            scale: true,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            data: param.date.map((str: any) => {
              return format(str, 'YYYY-MM-DD hh:mm a');
            }),
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(182, 202, 227)'
              }
            },
            axisLabel: {
              show: true,
              interval: 0,
              rotate: 90,
              formatter: ((data: any) => {
                return (data).slice(11, 19);
              })
            }
          },
          {
            gridIndex: 1,
            show: false,
            scale: true,
            type: 'category',
            boundaryGap: false,
            axisLine: {
              show: false
            },
            data: param.date,
            axisTick: {
              show: false
            },
            splitLine: {
              show: true
            }
          }],
          series: [{
            name: 'Humidity',
            data: param.humidity,
            type: 'line',
            itemStyle: {
              color: 'rgba(0, 101, 144, 1)'
            },
            markPoint: {
              type: 'Pin',
              data: [
                {
                  type: 'max',
                  itemStyle: {
                    color: 'rgba(0, 101, 144)'
                  }
                },
                {
                  type: 'min',
                  itemStyle: {
                    color: 'rgb(110, 151, 204)'
                  }
                }
              ]
            },
            smooth: true,
            xAxisIndex: 1,
            yAxisIndex: 1
          },
          {
            name: 'Temperature',
            data: param.temperature,
            type: 'line',
            itemStyle: {
              color: 'rgba(255, 0, 0, 1)'
            },
            markPoint: {
              type: 'Pin',
              data: [
                {
                  type: 'max',
                  itemStyle: {
                    color: 'rgba(255, 5, 0)'
                  }
                },
                {
                  type: 'min',
                  itemStyle: {
                    color: 'rgb(255, 87, 86)'
                  }
                }
              ]
            },
            smooth: true
          },

          ]
        };
        this.chartOption.push(option);
        this.notScrolly = true;
        this.tempThermometer.value.filter((x: any) => params.map((y: any) => {
          if (y.id === x.id) {
            return y.spinning = false;
          }
        }));
      });
    });
  }