如何使用angularjs刷新从REST服务器检索的实时数据?

时间:2022-12-23 03:53:50

I would like to retrieve table data from a REST backend server with angularjs. The data changes by the second. I would like to use angularjs to refresh the data as it changes in real-time. How can this be done? Do I force angularjs to make ajax calls at regular time intervals to refresh the data display?

我想从带有angularjs的REST后端服务器检索表数据。数据改变了第二个。我想使用angularjs来刷新数据,因为它实时变化。如何才能做到这一点?我是否强制angularjs以固定的时间间隔进行ajax调用以刷新数据显示?

2 个解决方案

#1


2  

Yeah with REST there's no other way then polling. To refresh the data itself in the browser view you can use $rootScope.$apply() in the service (I presume that you're using a service to get the data), first you have to inject the dependency of $rootScope of course.

是的,使用REST,然后没有其他方式进行轮询。要在浏览器视图中刷新数据本身,您可以在服务中使用$ rootScope。$ apply()(我假设您正在使用服务来获取数据),首先您必须注入$ rootScope的依赖关系。

EDIT Actually you shouldn't use $rootScope.$apply(), because it can lead to ugly $digest in progress errors. Instead use

编辑其实你不应该使用$ rootScope。$ apply(),因为它可能导致丑陋的$ digest进度错误。而是使用

$timeout(function(){
  // set data here...
})

A tip for improvement if you're not happy with the polling and if you have programmed the backend or are in position to change it, is: Try to use WebSockets:

如果您对轮询不满意,并且如果您已编程后端或有能力更改它,则需要改进提示:尝试使用WebSockets:

  • It gets rid of the polling, because then your browser can communicate directly to the server.
  • 它摆脱了轮询,因为那时你的浏览器可以直接与服务器通信。
  • It has less overhead.
  • 它的开销较小。
  • It is supported in the major browsers and you can use libraries with fallback mechanisms like SocketIO
  • 它在主流浏览器中受支持,您可以使用具有SocketIO等回退机制的库

The server can then always push the data right then, when it's available. The server-side implementation depends on the backend you are using, but most backend frameworks/servers nowadays also support websockets.

然后,服务器可以随时将数据推送到可用状态。服务器端实现取决于您使用的后端,但现在大多数后端框架/服务器也支持websockets。

#2


2  

You have to either use the poll or push strategy. However, since you already know that the resource changes regularly, it should be enough to setup a recurring timeout so that your application polls the resource on the REST server every second. Once it is retrieved, AngularJS updates the view. So, yes you have to force AngularJS to make calls to the service.

您必须使用民意调查或推送策略。但是,由于您已经知道资源会定期更改,因此应该足以设置重复超时,以便应用程序每秒轮询REST服务器上的资源。检索后,AngularJS会更新视图。所以,是的,你必须强制AngularJS来调用服务。

#1


2  

Yeah with REST there's no other way then polling. To refresh the data itself in the browser view you can use $rootScope.$apply() in the service (I presume that you're using a service to get the data), first you have to inject the dependency of $rootScope of course.

是的,使用REST,然后没有其他方式进行轮询。要在浏览器视图中刷新数据本身,您可以在服务中使用$ rootScope。$ apply()(我假设您正在使用服务来获取数据),首先您必须注入$ rootScope的依赖关系。

EDIT Actually you shouldn't use $rootScope.$apply(), because it can lead to ugly $digest in progress errors. Instead use

编辑其实你不应该使用$ rootScope。$ apply(),因为它可能导致丑陋的$ digest进度错误。而是使用

$timeout(function(){
  // set data here...
})

A tip for improvement if you're not happy with the polling and if you have programmed the backend or are in position to change it, is: Try to use WebSockets:

如果您对轮询不满意,并且如果您已编程后端或有能力更改它,则需要改进提示:尝试使用WebSockets:

  • It gets rid of the polling, because then your browser can communicate directly to the server.
  • 它摆脱了轮询,因为那时你的浏览器可以直接与服务器通信。
  • It has less overhead.
  • 它的开销较小。
  • It is supported in the major browsers and you can use libraries with fallback mechanisms like SocketIO
  • 它在主流浏览器中受支持,您可以使用具有SocketIO等回退机制的库

The server can then always push the data right then, when it's available. The server-side implementation depends on the backend you are using, but most backend frameworks/servers nowadays also support websockets.

然后,服务器可以随时将数据推送到可用状态。服务器端实现取决于您使用的后端,但现在大多数后端框架/服务器也支持websockets。

#2


2  

You have to either use the poll or push strategy. However, since you already know that the resource changes regularly, it should be enough to setup a recurring timeout so that your application polls the resource on the REST server every second. Once it is retrieved, AngularJS updates the view. So, yes you have to force AngularJS to make calls to the service.

您必须使用民意调查或推送策略。但是,由于您已经知道资源会定期更改,因此应该足以设置重复超时,以便应用程序每秒轮询REST服务器上的资源。检索后,AngularJS会更新视图。所以,是的,你必须强制AngularJS来调用服务。