将对象推入可观察订阅中的数组

时间:2020-04-14 18:53:37

标签: angular typescript rxjs observable

我有两个请求具有相同的对象结果(“数据”)。我将得到两个响应并将所有响应放入数组(“ Data []”)。例子:

为我服务:

  getData1(): Observable<Data> {
    return this.http
      .get<Data>(`${url}/data`):

  }

  getData2(): Observable<Data> {
    return this.http
      .get<Data>(`${url}/data`):

  }

如何获取两种方法的结果并将所有内容都放入组件的data []中?

更新解决方案

服务

  getData1(): Observable<Data> {
    return this.http
      .get<Data>(`${url}/data`):

  }

  getData2(): Observable<Data> {
    return this.http
      .get<Data>(`${url}/data`):

  }

 getDatas(): Observable<GetData[]> {
   return forkJoin([this.getData1(), this.getData2()]);
  }

组件

 getDatas: GetData[];
 ngOnInit() {
    this.dataService.getDatas().subscribe(x =>  this.getDatas= x);
  }

我可以在* ngFor =“让数据中的数据进行循环”

正在工作

1 个答案:

答案 0 :(得分:2)

使用function [ acf ] = corrFFT(samplesMat,denomType,mu) % Adaptation of inbuilt function autocorr to allow for multiple % realisations, known mean and variable denominator. % % Input: % samplesMat - nx by nSamples matrix of realisations % denomType (optional) - 'cnst' or 'var' % mu (optional) - if true mean is known if nargin < 2 denomType = 'cnst'; end if nargin < 3 mu = mean(mean(samplesMat)); end [nx,~] = size(samplesMat); nFFT = 2^(nextpow2(nx)+1); F = fft(samplesMat-mu,nFFT); F = F.*conj(F); acf = ifft(mean(F,2)); acf = acf(1:nx); if strcmp(denomType,'var') acf = (nx./[nx:-1:1]').*acf; end acf = acf./acf(1); acf = real(acf); end 如下:

ForkJoin

这将以 getData(): Observable<Data[]> { const req = []; req.push(this.http.get<Data>(url_1)); req.push(this.http.get<Data>(url_2)); return forkJoin(req); } 的形式返回响应,其中将提供两个网址的响应

相关问题