1

I'd like to display some data stored in a Firebase database. I've got no problem getting the data into the scope but it isn't displayed to the user.

    var ref = new Firebase('https://URL.firebaseio.com/users');

    $scope.users = [];

    ref.once("value", function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var dataUser = childSnapshot.val()
            dataUser.id = childSnapshot.key()
            $scope.users.push(dataUser);
        });
    });

The ng-inspector Chrome extension does confirm that the data is loaded into the scope.

enter image description here

What am I missing ?

Thanks.

3
  • Try using $scope.$apply; to have angularjs update the digest loop Commented Mar 29, 2016 at 14:43
  • Try if (!$scope.$$phase && !$scope.$root.$$phase) { $scope.$digest(); } after forEach loop Commented Mar 29, 2016 at 14:47
  • @inspired's answer will work. An alternative is to use AngularFire, which internally calls $timeout() whenever needed. Commented Mar 29, 2016 at 15:08

1 Answer 1

1

Services like $http and $setTimeout automatically call $apply() method for you when making asynchronous changes to the $scopeso that the DOM will be updated. You will need to call $scope.$apply(); to trigger the $digest loop.

You could do:

var ref = new Firebase('https://URL.firebaseio.com/users');

$scope.users = [];

ref.once("value", function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
        var dataUser = childSnapshot.val()
        dataUser.id = childSnapshot.key()
        $scope.users.push(dataUser);
    });
    $scope.$apply(); //let angular know to trigger $digest loop to update the DOM.
});
Sign up to request clarification or add additional context in comments.

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.