0

I'm trying to display response of firebase realtime database to HTML page with AngularJS as follow {{ pps }}.

Controller.js

firebase.database().ref('/activeUsers/9791009080').once('value').then(function(snapshot) {
    $scope.pps = (snapshot.val() && snapshot.val().msisdn) || 'Anonymous';
    console.log($scope.pps);
});

index.html

<h1>{{ pps }}</h1>

Problem is frontend HTML value is not reflected when I've found value inside console.log Please let me know what I've missed above code.

3 Answers 3

2

Use $scope.$apply to tell angular about the changes.

firebase.database().ref('/activeUsers/9791009080').once('value').then(function(snapshot) {
  $scope.$apply(() => {
    $scope.pps = (snapshot.val() && snapshot.val().msisdn) || 'Anonymous';
    console.log($scope.pps);
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

If your new to using Angular 1 + Firebase, I would recommend you to try out AngularFire. It does a better integration than your vanilla angular approach.

Check it out here.

https://github.com/firebase/angularfire/blob/master/docs/quickstart.md

Peace.

Comments

1

You can try below solution.

firebase.database().ref('/activeUsers/9791009080').once('value').then(function(snapshot) {
    $timeout(function() {
        $scope.pps = (snapshot.val() && snapshot.val().msisdn) || 'Anonymous';
    });
});

Now, why this?
Actually, the event will occur $apply scope as out scope so need to tell angular that there is some event to handle. You can do this via $timeout or you can try $apply scope.

Here, I'm using $timeout because this will doesn't throw an error even though it will maintain the flow while event from firebase occured.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.