【问题标题】:How can I use $scope in $on event handler如何在 $on 事件处理程序中使用 $scope
【发布时间】:2018-02-05 00:21:48
【问题描述】:

我正在使用 Angular 和 Firebase。我想从函数Datasnapshot$scope.getData 中获取值,但为什么我不能呢?请告诉我。谢谢。

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

【问题讨论】:

    标签: javascript angularjs firebase angularjs-scope


    【解决方案1】:

    在函数中注入$scope

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

    【讨论】:

    • 因为你需要在console.log中通过$scope.getData访问getData,但是你没有这样做
    • 是的,先生,但我像你一样在函数中注入 $scope,但它是一样的
    • 不仅注入还要添加console.log($scope.getData);而不是console.log(getData);
    • 哦,谢谢,但为什么我不能在视图中使用 $scope.getData
    猜你喜欢
    • 2023-03-08
    • 2015-02-14
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2015-01-17
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多