Javascript Firebase数据库功能可以';t在angularjs中修改$scope变量

Javascript Firebase数据库功能可以';t在angularjs中修改$scope变量,javascript,angularjs,firebase,firebase-realtime-database,Javascript,Angularjs,Firebase,Firebase Realtime Database,我有以下代码: angular.module("MyApp", ['textAngular']) .controller("demoController", function demoController($scope) { $scope.content = null; firebase.database().ref('pages/home').on('value', function(snapshot) { $scope.content = snapshot.val(); })

我有以下代码:

angular.module("MyApp", ['textAngular'])
.controller("demoController", function demoController($scope) {
    $scope.content = null;
    firebase.database().ref('pages/home').on('value', function(snapshot) { $scope.content = snapshot.val(); });
    console.log($scope.content);
});
如您所见,我所要做的就是在访问firebase数据库的函数中更改控制器范围内的变量,以便存储某个条目的内容。但是,尝试此操作时,变量不会发生更改。我用“window.content”和“var-content;”做了同样的尝试无济于事。是否有任何方法允许从函数中全局访问变量

编辑-当我运行firebase.database().ref函数并使用alert(snapshot.val())时;它起作用,信息显示在警报中。

Firebase是异步的(即剩余代码将在不等待Firebase函数运行的情况下运行)。将console.log放入on函数中,您将看到范围变量实际上已更改。

Firebase是异步的,(即剩余代码将在不等待firebase函数运行的情况下运行。将console.log放入on函数中,您将看到范围变量实际上已更改。

使用
$apply()

$apply()
用于从外部以角度执行表达式 角度框架(例如来自浏览器DOM事件、setTimeout、XHR或第三方库)

例如:

firebase.database()
  .ref('pages/home')
  .on('value', function(snapshot) {     
    $scope.$apply(function(){
      $scope.content = snapshot.val();
    });
});
使用
$apply()

$apply()
用于从外部以角度执行表达式 角度框架(例如来自浏览器DOM事件、setTimeout、XHR或第三方库)

例如:

firebase.database()
  .ref('pages/home')
  .on('value', function(snapshot) {     
    $scope.$apply(function(){
      $scope.content = snapshot.val();
    });
});
试试这个

firebase.database().ref('pages/home').on('value', function(snapshot) {     scope.$apply(function(){$scope.content = snapshot.val().content });});
试试这个

firebase.database().ref('pages/home').on('value', function(snapshot) {     scope.$apply(function(){$scope.content = snapshot.val().content });});

这些方法都不起作用,但我最终通过这样做解决了问题:

var app = angular.module("textAngularDemo", ['textAngular'])
app.controller("demoController", function demoController($scope) {
    firebase.database().ref('pages/home').on('value', function(snapshot) {     
        test($scope, snapshot.val());
    });
    $scope.detectChange = function(){
        setTimeout(function(){
            if ($scope.content)
            {
                console.log ($scope.content);
                }
            }
            else
            {
                $scope.detectChange();
            }
        }, 1);
    }
    function test(scope, variable){
        scope.content = variable;
        $scope.$apply();
    }
    setTimeout(function(){ $scope.detectChange() }, 1);
});

我相信这与承诺的作用是一样的,但是我当时不知道如何实现它们。如果你愿意,可以称之为“伪承诺”。

这些方法都不起作用,但我最终通过这样做解决了问题:

var app = angular.module("textAngularDemo", ['textAngular'])
app.controller("demoController", function demoController($scope) {
    firebase.database().ref('pages/home').on('value', function(snapshot) {     
        test($scope, snapshot.val());
    });
    $scope.detectChange = function(){
        setTimeout(function(){
            if ($scope.content)
            {
                console.log ($scope.content);
                }
            }
            else
            {
                $scope.detectChange();
            }
        }, 1);
    }
    function test(scope, variable){
        scope.content = variable;
        $scope.$apply();
    }
    setTimeout(function(){ $scope.detectChange() }, 1);
});

我相信这与承诺的作用方式相同,但是我当时不知道如何实现它们。如果愿意,可以称之为“伪承诺”。

尝试此操作后,变量仍然无法识别,并且在记录时返回null。尝试此操作后,变量仍然无法识别,并且在记录时返回null。