0

I am trying to make a comments section on my website. I have properly linked all my files and getting data from Firebase is working. But, the comments only load after I either click on something or enter text in the "Add comment" field; it does not show the comments instantly when the page loads. Does anyone know why?

Relevant code: (placed at the top of my controller)

$scope.comments = {};
database.ref('comments').on('value', function(items) {
  $scope.comments = items.val();
});
$scope.comment = function() {
  database.ref('comments').push($scope.newComment);
  $scope.newComment = "";
};
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Neil A.
  • 841
  • 7
  • 23

1 Answers1

1

Use $scope.$apply

$scope.comments = {};
database.ref('comments').on('value', function(items) {  
   $scope.$apply(function () {
        $scope.comments = items.val();
   });
});
$scope.comment = function() {
database.ref('comments').push($scope.newComment);
    $scope.newComment = "";
};
Aditya Ch
  • 26
  • 4