我想了想,还是把所有的这种数据改成了observable:
PublicGetSensorMap(): Observable<Id2SensorMap> {
if(this.id2sensorMap.size !== 0)
return Observable.of(this.id2sensorMap);
return this.GetSensorsAll().map(o => {
for(const sensor of o)
this.id2sensorMap.set(+sensor.sen_id, sensor);
return this.id2sensorMap;
});
}
以后每个component在init的时候订阅这个函数的返回值,在回调里写逻辑
我说的同步的意思是代码上看上去是同步,异步的工作隐藏在后面
我现在还是只能写
serv.PublicGetSensorMap().subscribe(o => {
print o;
o.show();
........
});
这样,我觉得很不爽
【 在 hgoldfish (老鱼) 的大作中提到: 】
: 标 题: Re: angular里,怎么做到http请求同步
: 发信站: 水木社区 (Thu Oct 12 15:26:25 2017), 站内
:
: 同步请求?那页面得卡死啊?请求天生异步。
:
: 建议你上 es2017 的 async/await 不纠结。我们已经这么搞了很久了。不过,需要注意`await $http()`之后执行的语句修改 $scope 无效。
:
: 【 在 libgcc (WS “净坛使者”) 的大作中提到: 】
: : 问题是,我想直接用data不想把接口换成promise或者observable,还要再订阅
: : 就没有一种机制,同步状态下
: : 我请求一个变量,它已经赋值了就直接返回,没有的话就做一个同步请求完了后赋值再返回
: : ...................
:
: --
: 灭绝人性啊
:
:
: ※ 修改:·hgoldfish 于 Oct 12 15:27:39 2017 修改本文·[FROM: 27.154.27.*]
: ※ 来源:·水木社区 newsmth.net·[FROM: 27.154.27.*]
--
修改:hgoldfish FROM 27.154.27.*
FROM 111.47.10.*