AngularJS服务器和客户端之间的自动同步数据 [英] AngularJS Automatically Syncing Data between Server and Client

查看:173
本文介绍了AngularJS服务器和客户端之间的自动同步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么东西在AngularJS文档,我似乎无法找到或也许我只是缺少。

There's something in the AngularJS docs that I can't seem to find or maybe I'm just missing.

我创建与后端和的NodeJS前preSS一个web应用程序,我试图了解它如何在前端与角进行交互。特别是,我将有一个JSON API的角度来从信息。我想对于前端始终保持最新。

I'm creating a web app with NodeJS and Express in the back-end and I'm trying to understand how it can interact with Angular in the front end. In particular, I'll have a JSON API for Angular to fetch the information from. I want for the front end to always be up-to-date.

我的问题是:


  • 是否数据绑定功能的双向平均角($资源或
    $ HTTP)自动从服务器获取数据,每一个的 N
    秒?

  • 是否自然地使用长轮询,短轮询或WebSockets的?

  • 请你需要jQuery来实现服务器 - 客户端同步或者一切可以做
    有角?

  • 请你必须添加额外的code键使这种行为
    发生?我是否需要使用$超时?

我似乎找到每一个例子涉及客户端获取一次数据。不同步与服务器的数据。

Every example I seem to find involves the client fetching the data once. Not syncing the data with the server.

推荐答案

双向在AngularJS绑定是指数据模型($范围)和视图(指令)。例如,如果在你的模型数据的变化,视图将自动更新。同样,如果用户在视图中修改数据,模型将自动更新。

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

与Web服务的交互是通过HTTP $服务模块进行。所以,从你的RESTful API获取数据,你会做这样的事情:

Interacting with web services is conducted via the $http service module. So to get data from your RESTful API, you would do something like:

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

为$ HTTP的完整文档,在 AngularJS网站。我想你会发现这是非常类似jQuery的$。阿贾克斯的方法。您可以轻松地设置$ http.get()短轮询角的$超时服务(基本上是一个的setTimeout包装)。

Full documentation for $http is on the AngularJS site. I think you will find it is very similar to jQuery's $.ajax methods. You easily set up $http.get() for short-polling with angular's $timeout service (basically a wrapper for setTimeout).

有关的AngularJS客户端和服务器之间的API实时更新,你可能想看看 Socket.io 。它使用的node.js创建浏览器和服务器之间的实时套接字连接,并有旧的浏览器回退机制(闪,长轮询)。有演示如何建立与AngularJS Socket.io在GitHub上一个样板工程:
<一href=\"https://github.com/btford/angular-socket-io-seed\">https://github.com/btford/angular-socket-io-seed

For real time updates between the AngularJS client and the server API, you might want to look into Socket.io. It uses node.js to create a live socket connection between the browser and the server, and has fallback mechanisms (flash, long-polling) for older browsers. There is a boilerplate project on GitHub that demonstrates how to set up AngularJS with Socket.io: https://github.com/btford/angular-socket-io-seed

要回顾:

数据是否绑定功能的双向平均角($资源或
  $ HTTP)自动从服务器获取数据,每N秒?

Does the two-way data binding feature mean Angular ($resource or $http) automatically fetches the data from the server every n seconds?

没有,双向绑定是角模型和视图之间。

No, two-way binding is between Angular models and views.

是否自然地使用长轮询,短轮询或WebSockets的?

Does it naturally use long polling, short polling or websockets?

角默认情况下不包括任何这些。你必须自行设置。

Angular does not include any of these by default. You must set them up yourself.

您是否需要jQuery来实现服务器 - 客户端同步,也可以与一切角做什么?

Do you need JQuery to achieve the server-client syncing or can everything be done with Angular?

$ HTTP是,在广义上,的角相当于jQuery的$就

$http is, in a broad sense, the Angular equivalent of jQuery's $.ajax

你必须添加额外的code键使这种行为发生的呢?我是否需要使用$超时?

Do you have to add extra code to make this behavior happen? Do I need to use $timeout?

使用$超时短投票,或推出自己的解决方案为长轮询和/或WebSockets的(见的角座-io的种子项目)。

Use $timeout for short-polling, or roll your own solution for long-polling and/or websockets (see the angular-socket-io-seed project).

这篇关于AngularJS服务器和客户端之间的自动同步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆