AngularJS Automatic data synchronization between server and client

There is something in AngularJS docs that I cannot find, or maybe just missing.

I am building a web application with NodeJS and Express in the background, and I'm trying to figure out how it can interact with Angular in the interface. In particular, I will have a JSON API for Angular to get the information. I want the front end to always be up to date.

My questions:

  • Does the two-way data binding function mean Angular ($ resource or $ http) automatically retrieves data from the server every n seconds?
  • Is it natural to use long polls, short polls, or websites?
  • Do you need jQuery to achieve synchronization with the server and client, or can everything be done with Angular?
  • Need to add extra code to make this behavior happen? Do I need to use $ timeout?

Each example that I think it finds is that the client retrieves the data once. Do not synchronize data with the server.

+57
angularjs angular-resource
Mar 14 '13 at 18:03
source share
1 answer

Two-way binding in AngularJS refers to the data model ($ scope) and your view (directives). For example, if the data changes in your model, the view will automatically update. Similarly, if the user changes the data in the view, your model will automatically update.

Interaction with web services is carried out through the service module $ http. Thus, to get data from your RESTful API, you would do something like:

$http.get('/someUrl').success(successCallback); 

Full documentation for $ http is on the AngularJS website . I think you will find that it is very similar to jQuery $ .ajax methods. You easily configure $ http.get () for a short poll using the wail> w500> $ service (basically a wrapper for setTimeout).

For real-time updates between the AngularJS client and the server API, you can see Socket.io . It uses node.js to create a direct socket connection between the browser and the server and has flash (long, polling) mechanisms for older browsers. GitHub has a template project that demonstrates how to configure AngularJS using Socket.io: https://github.com/btford/angular-socket-io-seed

In reply:

Is there a two-way data binding function angular ($ resource or $ http) that automatically retrieves data from the server every n seconds?

No, two-way binding between models and angular views.

Does he naturally use a lengthy survey, a short survey, or websites?

Angular does not include any of them by default. You must configure them yourself.

Do you need jQuery to achieve server-client synchronization, or can everything be done using Angular?

$ http, in a broad sense, is the equivalent of angular jQuery $ .ajax

Do you need to add extra code for this to happen? Do I need to use $ timeout?

Use $ timeout for a short survey or minimize your own solution for long surveys and / or websites (see the angular-socket-io-seed project).

+81
Mar 14 '13 at 18:53
source share



All Articles