Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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
如何在Angular中实时异步地从RESTAPI获取数据_Angular_Rest_Rxjs_Observable - Fatal编程技术网

如何在Angular中实时异步地从RESTAPI获取数据

如何在Angular中实时异步地从RESTAPI获取数据,angular,rest,rxjs,observable,Angular,Rest,Rxjs,Observable,在我的组件文件中,我使用http客户端从api端点获取json文件的可观察性: this.data = this.http.get(api); 在模板中,我使用异步管道绑定数据字段: <td *ngFor="let post of (data | async)"> {{post.title}} </td> {{post.title} 现在我正在获取数据,问题是我希望它能够实时发生,而无需刷新页面。 那么,我是否可以实现异步数据加载 p.S 我已经尝试过一些解决方法

在我的组件文件中,我使用http客户端从api端点获取json文件的可观察性:

this.data =  this.http.get(api);
在模板中,我使用异步管道绑定数据字段:

<td *ngFor="let post of (data | async)"> {{post.title}} </td>
{{post.title}
现在我正在获取数据,问题是我希望它能够实时发生,而无需刷新页面。 那么,我是否可以实现异步数据加载

p.S

我已经尝试过一些解决方法,比如设置interval以持续订阅可观察对象,但这只会耗尽资源,而且似乎还有另一种更干净的方法我没有做到。

您无法在前端做任何事情来实现实时性。 异步管道将密切关注响应,但为了获得响应,您必须点击 API在一段时间间隔或类似的时间之后。正如你已经说过的,这不是更清洁的解决方案。您可以使用web套接字实现这一点,它是后端。
默认情况下,某些数据库本质上是实时的。Firebase就是其中之一。

你的意思是不断点击端点获取新数据吗?如果你真的想要实时,你可以使用WebSocket。或者,您可以使用轮询机制,在这种机制中,您可以每1或2秒点击一次API。