Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularFire$loaded永不开火_Javascript_Angularjs_Ionic Framework_Angularfire - Fatal编程技术网

Javascript AngularFire$loaded永不开火

Javascript AngularFire$loaded永不开火,javascript,angularjs,ionic-framework,angularfire,Javascript,Angularjs,Ionic Framework,Angularfire,我正在尝试做一些应该相当简单的事情——从Firebase DB加载一个对象,并将其分配给控制器中的一个变量。在我使用Firebase函数(请参阅注释掉的代码)之前,这种方法非常有效,但是我想使用3路绑定,所以我尝试在AngularFire中做同样的事情 $loaded事件似乎没有触发,因为planner对象(或“You is Here”)从未打印到控制台。user.uid字符串按预期打印到控制台,注释掉的代码工作正常。提前感谢你的帮助 (附带问题:在注释掉的代码中,我使用evalAsync来确保

我正在尝试做一些应该相当简单的事情——从Firebase DB加载一个对象,并将其分配给控制器中的一个变量。在我使用Firebase函数(请参阅注释掉的代码)之前,这种方法非常有效,但是我想使用3路绑定,所以我尝试在AngularFire中做同样的事情

$loaded事件似乎没有触发,因为planner对象(或“You is Here”)从未打印到控制台。user.uid字符串按预期打印到控制台,注释掉的代码工作正常。提前感谢你的帮助

(附带问题:在注释掉的代码中,我使用evalAsync来确保在检索信息时网页上的表单实际上是更新的——这是一种好做法吗?)

我的控制器:

   firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            console.log("Currently logged in user...");
            console.log(user.uid);
            $scope.user = user;

           var ref = firebase.database().ref('planners/' + $scope.user.uid);
           var planner = $firebaseObject(ref);

           planner.$loaded().then(function() {
              console.log("You are here")
              console.log(planner)
           })
           .catch(function(error) {
              console.log(error)
           })
           // firebase.database().ref('planners/' + $scope.user.uid).on('value', function(response) {
           //    if (response.val() != null) {
           //       $scope.$evalAsync(function() {
           //          $scope.planner = response.val();
           //       })
           //    }
           // });
        }
        else {
           console.log('No user logged in')
           $state.go('login')
        }
    })
试着改变这个

firebase.auth().onAuthStateChanged(function(user) {
对此

$firebaseAuth().$onAuthStateChanged(function(user) {
有关更多信息,请参阅文档

尝试更改此

firebase.auth().onAuthStateChanged(function(user) {
对此

$firebaseAuth().$onAuthStateChanged(function(user) {
有关更多信息,请参阅文档


正如@Aaron Saunders所建议的,您应该使用AngularFire auth对象,因为它更好地与Angular集成(更新
$scope
s、触发器
$digest
s等)

您以前使用的代码之所以有效,是因为
$evalAsync
在没有运行时会触发
$digest
$digest
循环是绑定模型和视图之间数据的魔术师)

实际上,您应该使用AngularFire绑定数据。它们还通过利用
$bindTo
提供“三向绑定”(模型视图数据库)

正如您在
$firebaseObject
中看到的,数据将自动从服务器同步到客户端,但如果您想将本地更改保存回服务器,则应使用或

代码修复建议:

$firebaseAuth().$onAuthStateChanged(function(user) {
    // By using `$firebaseAuth` this function will be called in a `$digest` so all changes will be bound
    if (user) {
        console.log("Currently logged in user...");
        console.log(user.uid);
        $scope.user = user;

        var ref = firebase.database().ref('planners/' + $scope.user.uid);
        // `$destroy` previous bindings
        if ($scope.planner && angular.isFunction($scope.planner.$destroy)) {
            $scope.planner.$destroy();
        }
        $scope.planner = $firebaseObject(ref);

        // planner.$loaded().then(function() {
        //    // This code will happen only after `$digest` since its promise is relied on the `$q` service
        //    console.log("You are here")
        //    console.log(planner)
        // })
        // .catch(function(error) {
        //    console.log(error)
        // })
        // firebase.database().ref('planners/' + $scope.user.uid).on('value', function(response) {
        //    if (response.val() != null) {
        //       $scope.$evalAsync(function() {
        //          $scope.planner = response.val();
        //       })
        //    }
        // });
    } else {
        console.log('No user logged in');
        $scope.user = null;
        if ($scope.planner && angular.isFunction($scope.planner.$destroy)) {
            $scope.planner.$destroy();
        }
        $state.go('login');
    }
});

正如@Aaron Saunders所建议的,您应该使用AngularFire auth对象,因为它更好地与Angular集成(更新
$scope
s、触发器
$digest
s等)

您以前使用的代码之所以有效,是因为
$evalAsync
在没有运行时会触发
$digest
$digest
循环是绑定模型和视图之间数据的魔术师)

实际上,您应该使用AngularFire绑定数据。它们还通过利用
$bindTo
提供“三向绑定”(模型视图数据库)

正如您在
$firebaseObject
中看到的,数据将自动从服务器同步到客户端,但如果您想将本地更改保存回服务器,则应使用或

代码修复建议:

$firebaseAuth().$onAuthStateChanged(function(user) {
    // By using `$firebaseAuth` this function will be called in a `$digest` so all changes will be bound
    if (user) {
        console.log("Currently logged in user...");
        console.log(user.uid);
        $scope.user = user;

        var ref = firebase.database().ref('planners/' + $scope.user.uid);
        // `$destroy` previous bindings
        if ($scope.planner && angular.isFunction($scope.planner.$destroy)) {
            $scope.planner.$destroy();
        }
        $scope.planner = $firebaseObject(ref);

        // planner.$loaded().then(function() {
        //    // This code will happen only after `$digest` since its promise is relied on the `$q` service
        //    console.log("You are here")
        //    console.log(planner)
        // })
        // .catch(function(error) {
        //    console.log(error)
        // })
        // firebase.database().ref('planners/' + $scope.user.uid).on('value', function(response) {
        //    if (response.val() != null) {
        //       $scope.$evalAsync(function() {
        //          $scope.planner = response.val();
        //       })
        //    }
        // });
    } else {
        console.log('No user logged in');
        $scope.user = null;
        if ($scope.planner && angular.isFunction($scope.planner.$destroy)) {
            $scope.planner.$destroy();
        }
        $state.go('login');
    }
});