从 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 = 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);
            }
        });
}]);

我在代码中插入了帖子信息的条件,但这并不重要......我想要的只是实时更新信息,而无需重新加载我的页面。

谢谢你的帮助!

函数once触发一次,然后不会再次触发。这就是您的信息不会更新的原因。请改用on来订阅更改。更多信息在这里: https://firebase.google.com/docs/database/web/retrieve-data#listen_for_events

最新更新