从AngularJS的Firebase实时更新信息 [英] Update infromation in Real-Time from Firebase in AngularJS

查看:155
本文介绍了从AngularJS的Firebase实时更新信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AnguarJS从Firebase获取了展示信息的应用。在这一刻,信息刷新页面后更新,但我想实时更新我的​​信息。



这是我的角码:

  var root = angular.module('root',[services,firebase]); 
root.controller(root4,['$ sce',$ scope,function($ sce,$ scope){
var ref = new Firebase(https:// web-quickstart -8326d.firebaseio.com);
var locationRef = rootRef.child('location');
locationRef.once(value,function(snapshot){
$ scope.value ();
$ scope。$ apply();

if($ scope.value ==col6){
var currentMessageRef = rootRef.child ('currentMessage');
currentMessageRef.once(value,function(snapshot){
$ scope.html = snapshot.val(); $ b $ scope.trustedHtml = $ sce。 ($ scope);
});
} else if($ scope.value!=col6){
$ scope.html =db中没有值;
$ scope.trustedHtml = $ sce.trustAsHtml($ scope.html);
}
});
}]);

我在代码中插入发布信息的条件,但这并不重要...所有我想要的它是实时更新信息,无需重新加载我的网页。



感谢您的帮助!

解决方案

函数一次触发一次,然后不再触发。这就是为什么你的信息不会更新。在上使用代替订阅更改。更多信息: https://firebase.google.com/docs/database / web / retrieve-data#listen_for_events


I made a applicaion for show information from Firebase with AnguarJS. For this moment, information is updated after refresh page, but i want to update my information in real time.

Here is my angular code:

var root = angular.module('root',["services", "firebase"]);
    root.controller("root4",['$sce', "$scope", function($sce,$scope){ 
        var ref = new Firebase("https://web-quickstart-8326d.firebaseio.com");
        var locationRef = rootRef.child('location');
        locationRef.once("value", function(snapshot) {
            $scope.value = snapshot.val();
            $scope.$apply(); 

            if($scope.value=="col6"){
                var currentMessageRef = rootRef.child('currentMessage');
                currentMessageRef.once("value", function(snapshot) {
                    $scope.html = snapshot.val();
                    $scope.trustedHtml = $sce.trustAsHtml($scope.html);
                });
            } else if($scope.value!="col6"){
                $scope.html  = "No value in db";
                $scope.trustedHtml = $sce.trustAsHtml($scope.html);
            }
        });
}]);

I insert in my code a condition for post information, but that's not important... all i want it's to update information in real time without reload my page.

Thank you for help!

解决方案

Function once triggers once and then doesn't trigger again. That's why your information doesn't update. Use on instead to subscribe on changes. More info here: https://firebase.google.com/docs/database/web/retrieve-data#listen_for_events

这篇关于从AngularJS的Firebase实时更新信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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