Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度-当两个订阅已更新时检测_Javascript_Angular_Typescript_Promise_Subscribe - Fatal编程技术网

Javascript 角度-当两个订阅已更新时检测

Javascript 角度-当两个订阅已更新时检测,javascript,angular,typescript,promise,subscribe,Javascript,Angular,Typescript,Promise,Subscribe,在一个组件中,在ngOnInit()中,我有两个数据服务订阅。我想在两个订阅都返回后进行一些处理。最好的方法是什么?我可以在每次结束时进行处理,这似乎有点低效,而且无论哪个订阅先激活都不起作用 谢谢 组件技术 ngOnInit() { this.dataService.dataA().subscribe((dataAJSON) => { this.dataA= dataAJSON } this.dataService.dataB().subscribe((dataBJS

在一个组件中,在ngOnInit()中,我有两个数据服务订阅。我想在两个订阅都返回后进行一些处理。最好的方法是什么?我可以在每次结束时进行处理,这似乎有点低效,而且无论哪个订阅先激活都不起作用

谢谢

组件技术

ngOnInit()
{
  this.dataService.dataA().subscribe((dataAJSON) => 
 {
   this.dataA= dataAJSON
 }

 this.dataService.dataB().subscribe((dataBJSON) => 
 {
   this.dataB= dataBJSON
 }
数据服务

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()

export class PMDataService
{
  constructor(public http : Http)
  {

  }

  dataA()
  {

    var dataA: any;
    var json;

    dataA= this.http.get("./assets/dataa.json")
      .map(res => res.json());

    return dataA
  }

  dataB()
  {

    var dataB: any;
    var json;

    dataB= this.http.get("./assets/datab.json")
      .map(res => res.json());

    return dataB
  }

}
你可以在观测值上使用函数。当所有可观测数据完成时,它会从每个数据中发出最后一个值

Observable.forkJoin(this.dataService.dataA(), this.dataService.dataB())
          .subscribe(val => /* val is an array */)
你可以在观测值上使用函数。当所有可观测数据完成时,它会从每个数据中发出最后一个值

Observable.forkJoin(this.dataService.dataA(), this.dataService.dataB())
          .subscribe(val => /* val is an array */)

您可以使用rxjs中的运算符
Zip
CombineTest

你可以这样做:

    Observable.zip(
        this.http.get("./assets/dataa.json"),
        this.http.get("./assets/dataa.json")
    .take(1)
    .map(values => [values[0].json(), values[1].json()])
    .subscribe(values => {
        // do something with my values
    });

您可以使用rxjs中的运算符
Zip
CombineTest

你可以这样做:

    Observable.zip(
        this.http.get("./assets/dataa.json"),
        this.http.get("./assets/dataa.json")
    .take(1)
    .map(values => [values[0].json(), values[1].json()])
    .subscribe(values => {
        // do something with my values
    });
您可以使用组合观察值并返回单个观察值

按照前面完成的顺序订阅观测值,并发出值

更改服务代码

import 'rxjs/add/operator/concat';

export class PMDataService
{
  data(){
    return this.dataA().concat(this.dataB());
  }
  // methods dataA and dataB are unchanged, some of the constructor
}
组件代码

ngOnInit(){
  this.dataService.data().subscribe((dataJSON) => 
     {
       this.dataA= dataAJSON[0];
       this.dataB= dataAJSON[1];
     }
}
您可以使用组合观察值并返回单个观察值

按照前面完成的顺序订阅观测值,并发出值

更改服务代码

import 'rxjs/add/operator/concat';

export class PMDataService
{
  data(){
    return this.dataA().concat(this.dataB());
  }
  // methods dataA and dataB are unchanged, some of the constructor
}
组件代码

ngOnInit(){
  this.dataService.data().subscribe((dataJSON) => 
     {
       this.dataA= dataAJSON[0];
       this.dataB= dataAJSON[1];
     }
}

使用的方法取决于您希望接收数据的方式:

您可以使用该函数。当所有对象都发射一次时,发射一次。与“承诺”非常相似。除了未完成之外,所有的承诺都是“承诺”

Observable.zip(obs1, obs2).subscribe((val) => { ... });
您可以使用该函数。全部完成后发射一次。所以完全像
Promise.all

Observable.forkJoin(obs1, obs2).subscribe((val) => { ... });
您可以使用该函数。按发射顺序发射,因此可以是第一个然后是第二个或第二个然后是第一个:

obs1.merge(obs2).subscribe((val) => { ... });
您可以使用函数。按先发射后发射的顺序发射,无论第二次是否先发射:

obs1.concat(obs2).subscribe((val) => { ... });
为了清晰起见,最好将它们分成几行

const obs1 = Rx.Observable.of(1,2,3);
const obs2 = Rx.Observable.of(1,2,3);
const example = Observable.zip(obs1, obs2);
//const example = Observable.forkJoin(obs1, obs2);
//const example = obs1.merge(obs2);
//const example = obs1.concat(obs2);
example.subscribe(val => { ... });

使用的方法取决于您希望接收数据的方式:

您可以使用该函数。当所有对象都发射一次时,发射一次。与“承诺”非常相似。除了未完成之外,所有的承诺都是“承诺”

Observable.zip(obs1, obs2).subscribe((val) => { ... });
您可以使用该函数。全部完成后发射一次。所以完全像
Promise.all

Observable.forkJoin(obs1, obs2).subscribe((val) => { ... });
您可以使用该函数。按发射顺序发射,因此可以是第一个然后是第二个或第二个然后是第一个:

obs1.merge(obs2).subscribe((val) => { ... });
您可以使用函数。按先发射后发射的顺序发射,无论第二次是否先发射:

obs1.concat(obs2).subscribe((val) => { ... });
为了清晰起见,最好将它们分成几行

const obs1 = Rx.Observable.of(1,2,3);
const obs2 = Rx.Observable.of(1,2,3);
const example = Observable.zip(obs1, obs2);
//const example = Observable.forkJoin(obs1, obs2);
//const example = obs1.merge(obs2);
//const example = obs1.concat(obs2);
example.subscribe(val => { ... });