简体   繁体   中英

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);
    });             
});      

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);
        });

      }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM