Javascript 等待未完成的任务';t触发器变化检测
我正在实现一个角度通用应用程序 我想Javascript 等待未完成的任务';t触发器变化检测,javascript,node.js,angular,typescript,Javascript,Node.js,Angular,Typescript,我正在实现一个角度通用应用程序 我想transferState将数据从应用程序的后端传输到前端。 不幸的是,我正在Angular应用程序外部的后端检索数据(检索数据不会触发更改检测),在回调函数中,我可以操作检索到的数据。然后我尝试在解析器类中解析给定的可观察的 我的函数就是这样的: public someFunction(): Observable<string> { const key: StateKey<string> = makeStateKey<s
transferState
将数据从应用程序的后端传输到前端。
不幸的是,我正在Angular应用程序外部的后端检索数据(检索数据不会触发更改检测),在回调函数中,我可以操作检索到的数据。然后我尝试在解析器
类中解析给定的可观察的
我的函数就是这样的:
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():可观察{
const-key:StateKey=makeStateKey('myToken');
返回可观察的。创建(主题=>{
if(isPlatformServer(this.platformId)){
此.performTaskOutsideAngular((err,resp)=>{
此.transferState.set(键、相应值);
主题。下一个(分别为价值);
subject.complete();
});
}否则{
subject.next(this.transferState.get(key'null');
subject.complete();
}
});
}
我如何通知angular,它应该等待给定的任务完成?我正在寻找类似于:
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():可观察{
const-key:StateKey=makeStateKey('myToken');
返回可观察的。创建(主题=>{
if(isPlatformServer(this.platformId)){
此.ngZone.manualChangeDetection((appRef)=>{
此.performTaskOutsideAngular((err,resp)=>{
此.transferState.set(键、相应值);
主题。下一个(分别为价值);
subject.complete();
appRef.tick();
});
});
}否则{
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);
}
public someFunction():可观察{
const-key:StateKey=makeStateKey('myToken');
返回可观察的。创建(主题=>{
if(isPlatformServer(this.platformId)){
此.performTaskOutsideAngular((err,resp)=>{
此.transferState.set(键、相应值);
主题。下一个(分别为价值);
subject.complete();
});
}否则{
subject.next(this.transferState.get(key'null');
subject.complete();
}
});
设置超时(()=>{
log('设置超时导致角度通用在重新设置参数之前等待');
}, 10000);
}
为什么不使用这个.zone.runOutsideAngular
我试过了。但不知道如何通知angular,它应该等到某个时刻执行返回值的更改检测/解析程序。angular zone有一些方法:如isStable
&onStable
,它们可能对您的情况有所帮助。您可以参考此处给出的答案:。请检查并让我知道您是否面临问题。@Rock我在寻找答案时看到了这个。不幸的是,这不是我的问题的解决方案。我想强制Angular等待数据检索。如果我有一个方法可以等到我手动点击勾选
,那就太完美了。