Update infromation in Real-Time from Firebase in AngularJS

旧街凉风 提交于 2019-12-24 00:19:56

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/38370135/update-infromation-in-real-time-from-firebase-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!