等待不会触发变更检测的任务

时间:2018-12-31 13:31:43

标签: javascript node.js angular typescript

我正在实现Angular Universal应用程序。

我想transferState从应用程序的后端到前端的数据。 不幸的是,我在Angular应用程序之外的后端中检索数据(检索数据不会触发更改检测),并且在回调函数中,我可以操纵检索到的数据。然后,我尝试解决Observable类中给定的Resolver

这就是我的函数的样子:

public someFunction(): Observable<string> {
    const key: StateKey<string> = makeStateKey<string>('myToken');

    return Observable.create(subject => {
      if (isPlatformServer(this.platformId)) {
        this.performTaskOutsideAngular((err, resp) => {
          this.transferState.set(key, resp.value);
          subject.next(resp.value);
          subject.complete();
        });
      } else {
        subject.next(this.transferState.get(key, 'null'));
        subject.complete();
      }
    });
}

我如何通知角度应等待给定任务完成?我正在寻找类似的东西:

public someFunction(): Observable<string> {
    const key: StateKey<string> = makeStateKey<string>('myToken');

    return Observable.create(subject => {
      if (isPlatformServer(this.platformId)) {

        this.ngZone.manualChangeDetection((appRef) => {

            this.performTaskOutsideAngular((err, resp) => {
              this.transferState.set(key, resp.value);
              subject.next(resp.value);
              subject.complete();
              appRef.tick();
            });
        });
      } else {
        subject.next(this.transferState.get(key, 'null'));
        subject.complete();
      }
    });
}

编辑:

一些调查:

public someFunction(): Observable<string> {
    const key: StateKey<string> = makeStateKey<string>('myToken');

    return Observable.create(subject => {
      if (isPlatformServer(this.platformId)) {
        this.performTaskOutsideAngular((err, resp) => {
          this.transferState.set(key, resp.value);
          subject.next(resp.value);
          subject.complete();
        });
      } else {
        subject.next(this.transferState.get(key, 'null'));
        subject.complete();
      }
    });

    setTimeout(() => {
         console.log('set timeout causes the Angular Universal to wait, before resolivng parameters');
    }, 10000);
}

0 个答案:

没有答案