如何尽快实现自动视图更新,因为是在AngularJs数据库的变化? [英] How to implement automatic view update as soon as there is change in database in AngularJs?

查看:177
本文介绍了如何尽快实现自动视图更新,因为是在AngularJs数据库的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与Grails框架AngularJs和使用MySQL数据库。

I am using AngularJs with Grails framework and using Mysql as Database.

我想在Facebook上的出现来实现,如自动视图更新功能。

I want to implement feature like automatic view update as occur on Facebook.

到现在为止我能够从Grails的控制器和填充视图发送JSON数据的角度控制。

Till now i am able to send JSON data to angular controller from Grails Controller and populating view.

但我怎么实现的功能就像有人更改数据库和实时的意见得到而不需要刷新页面更新。

But how do i implement feature like some one change the database and on real time the views get updated without reloading page.

我凸轮跨

轮询使用$超时并每隔X毫秒$ HTTP

"polling every X milliseconds using $timeout and $http"

这将是有效的实际应用?

will this be efficient for real world application?

请通过提出一些措施或角的实现,解决了我的问题帮帮我吧。

Please help me by suggesting some measures or Angular implementations to solve out my problem.

推荐答案

有关的意见自动更新中,我使用的Grails活动​​推插件,我建议你看看吧。
http://grails.org/plugin/events-push

For 'automatic' update of the views, I've used Grails Events Push Plugin, I suggest that you look at it. http://grails.org/plugin/events-push

这真的很容易将事件发送到浏览器和客户端听取他们的意见和更新与propper信息AngularJS范围。

It's really easy to send the events to the browser and in the client listen to them and update the AngularJS scope with the propper information.

示例

这是angularJS文件

An angularJS file

window.grailsEvents = new grails.Events('http://myAppUrl.com', {enableXDR:true,readResponsesHeaders:false});

/**
 * Module for listening to grails events
 */
angular.module('grailsEvents', []).factory('grailsEvents', function() {
    return window.grailsEvents
});

window.myModule = angular.module('myModule',['grailsEvents'])
   .run(function(){
       grailsEvents.on('myEvent',function(data){
          //Every time an event occurs, this will be executed
          console.log(data);
       });
   });

MyEvents.groovy(Grails的应用/ conf目录)

MyEvents.groovy (in grails-app/conf)

events = {
    'myEvent' browser:true
}

TestController.groovy

TestController.groovy (an example of a controller that sends an event)

class TestController{
    def index(){
       event(topic:'myEvent',data:MyDomain.list())
    }
}

这篇关于如何尽快实现自动视图更新,因为是在AngularJs数据库的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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