Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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
具有某些顺序下标的Angular2自定义观测值_Angular_Promise_Observable_Sequential - Fatal编程技术网

具有某些顺序下标的Angular2自定义观测值

具有某些顺序下标的Angular2自定义观测值,angular,promise,observable,sequential,Angular,Promise,Observable,Sequential,你知道解决我问题的办法吗? 我需要一个灵活的订阅序列,封装在一个可观察的模型中,如下所示: saveData() { return new Observable((observer) => { let success = true; if(example === true) { this.saveCallbarrings().subscribe((response) => { // this response wa

你知道解决我问题的办法吗? 我需要一个灵活的订阅序列,封装在一个可观察的模型中,如下所示:

saveData() {
    return new Observable((observer) => {
      let success = true;

      if(example === true) {
        this.saveCallbarrings().subscribe((response) => {
          // this response was ignored from angular
          success = (response === true);
        });
      }

      if(example2 === true) {
        this.saveCallbarrings().subscribe((response) => {
          // this response was ignored from angular too
           success = (response === true);
        });
      }

      // and so on ... in the end I need a result of all responses
      observer.next(success);
    });
  }
最后,我在submit方法中调用了这个“响应集合”的结果:

onSubmit() {
// Validations and others
...
if(this.isNew) {
        observable = this.create();
      } else {
        observable = this.update();
      }

      return observable.subscribe(success => {
        if(success == true) {
          let subscription = this.saveData().subscribe(successFinished => {
            // And here is the problem, because this var doesnt have the correct response
            if(successFinished === true) {
              this.alertService.success('ALERT.success_saved', {value: 'ALERT.success_edit_user', param: {user: this.user.username}});
            }
          });

          subscription.unsubscribe();
        }
      });
主要问题是angular不会等到在第一个代码块中订阅了“success”var。
为什么以及对我来说什么是更好的解决方案?

第一个问题:为什么它不起作用

因为每个订阅都是异步的。当您执行
this.saveCallbarrings().subscribe(…)
subscribe中的事情随时都可能发生(可能永远不会发生!),因此程序将继续执行下一条指令,即
observer.next(成功)
,其初始值为
success

第二个问题:对我来说最好的解决方案是什么

可观测对象必须处理这种异步的东西。在您的情况下,您需要的操作员是。该操作符允许您向他传递一个流数组,它将订阅所有流,当所有流都完成时,它将为您提供一个包含每个流的每个结果的数组。因此,您的代码将变成:

saveData() {
    return Rx.Observable.defer(() => {
        let streams = [];
        if(example === true) {
            streams.push(this.saveCallbarrings());
        }
        if(example2 === true) {
            streams.push(this.saveCallbarrings());
        }

        // And so on

        return Rx.Observable.forkJoin(streams);
    });
}
话虽如此,我不知道你为什么要订阅同一个
this.saveCallbarrings()
,举个例子,我想这只是为了让问题更简单


此外,这里我使用了
.defer()
而不是create。有了它,您可以返回另一个流,它将订阅该流并将其传递给观察者。执行
defer
与不执行(即设置流并返回forkJoin)之间的区别在于
defer
在有人订阅之前不会执行任何代码,因此副作用较少。

您不能等待可观察到的或承诺完成。您只能订阅它以在它完成或发出事件时获得通知。您能添加saveExtendedData服务方法吗?因为我觉得您可以在那里进行更改。对不起,我的第二个代码块中有一个错误。方法“this.saveExtendedData()”应该是“this.saveData()”。在save data方法中,不给success变量赋值,而是返回结果,但我需要observable中所有订阅的最终结果。非常感谢。你的解决方案成功地为我工作!