Search code examples
javascriptangularjsfirebaseangularjs-scope

How can I use $scope in $on event handler


I am using Angular and Firebase. I want to get values from function Datasnapshot to $scope.getData, but why I can't? Tell me please. Thanks.

$scope.$on('$routeChangeSuccess', function () {   
    var firebaseUrl ="https://angular-af216.firebaseio.com";
    var commentRef = new Firebase(firebaseUrl).child('User');

    commentRef.on('value', function(Datasnapshot) {
        var comments = Datasnapshot.val();
        // var data = Datasnapshot.child('User').val();
        console.log(comments);
        console.log("Newline");
        $scope.getData = comments;
        console.log(getData);
    });             
});      


Solution

  • Inject $scope in the function

    $scope.$on('$routeChangeSuccess', function ($scope) {   
            var firebaseUrl ="https://angular-af216.firebaseio.com";
            var commentRef = new Firebase(firebaseUrl).child('User');
    
            commentRef.on('value', function(Datasnapshot) {
            var comments = Datasnapshot.val();
            // var data = Datasnapshot.child('User').val();
            console.log(comments);
            console.log("Newline");
            $scope.getData = comments;
            console.log($scope.getData);
            });
    
          });