2

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!

adolfosrs
  • 9,286
  • 5
  • 39
  • 67
Diaconu Eduard
  • 161
  • 1
  • 3
  • 14
  • Have you tried calling `$scope.$apply()` after assigning the scope variables that are not displayed in real time? See also http://stackoverflow.com/questions/38366955/firebase-3-angularfire-2-in-ionic-app-data-not-updating-real-time-live-update/38368256#38368256 – Devid Farinelli Jul 14 '16 at 09:32
  • $scope.apply(), it's just for identificate where it's neccessary to insert a message. Practically, message it's show in if condiion. I want to update just message. Thank you for reply! :) – Diaconu Eduard Jul 14 '16 at 09:35
  • @DiaconuEduard add your html plz. It will be so nice if you set up a [jsFiddle](https://jsfiddle.net/vyp76wj1/17/) as well. – adolfosrs Jul 14 '16 at 14:47
  • @adolfosrs, thanks for reply. I successfully added the command for real time update. :) Have a nice day! – Diaconu Eduard Jul 15 '16 at 07:42

1 Answers1

2

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

Yevgen
  • 4,519
  • 3
  • 24
  • 34