Angular 2以“实时"更新对象. [英] Angular 2 Updating objects in “real time.”
问题描述
我想总结一下如何更新角度为2的桌子.
Hi I’m trying to wrap on how to update a table angular 2.
这是我所拥有的: 后端:express/MongoDB.通过外部应用程序将更新馈入数据库 数据:90%的数据将是静态的.每秒有10%的数据更新.
Here is what I have: Backend: express / MongoDB. Updates are feed into the DB via an external app Data: 90% data will will be static. 10% of the data updates every second.
我研究了可观察的/应允的. HTTP请求/套接字IO,但我无法确定这些概念.
I’ve looked at Observables / promises. HTTP requests/ socket IO but can’t wrap my mind around the concepts.
主要问题:我可以在socket.io中使用可观察对象来更新记录吗?
Main Question: can I use observables with socket.io to update records?
有关数据更新的其他问题
Other Questions about data updates
- Angular 2的可观察对象–仅当客户端提取数据时,可观察对象才使用吗?或者在将数据推送到客户端时可以将其与套接字一起使用. (所有在线示例都使用带有http请求的可观察对象)
- 您可以使用Socket IO更新对象还是仅用于新对象?看到的每个示例都是一个聊天应用程序.
- 使用http请求时,如何设置请求数据的频率? (一些示例在线使用循环,但这似乎是错误的.)
推荐答案
-
可观察对象是基于事件的,因此可用于利用Web套接字从服务器接收事件.看一下本文(基于事件的支持"部分):
Observables are event-based so they can be used to receive events from server leveraging web sockets. Have a look at this article (section "Event-based support"):
事实上,它是新对象,但是您可以利用scan
运算符来汇总多个事件的内容.
In fact it's new objects but you can leverage the scan
operators to aggregate the content of several events.
var obs = (...)
obs.startWith([])
.scan((acc,value) => acc.concat(value))
.subscribe((data) => {
console.log(data);
});
有关更多详细信息,请参见此问题:
See this question for more details:
如果要在一定时间间隔内拉动,可以利用interval
方法:
If you want to pull with a time interval, you can leverage the interval
method:
Observable.interval(3000).flatMap(() => {
return this.http.get('/some-request').map(res => res.json());
}).subscribe((data) => {
console.log(data);
});
这篇关于Angular 2以“实时"更新对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!