I am currently banging my head trying to understand a little better how Angularfire works in this case. I have two controllers on my "webapp / test", one for logging in and the other for displaying profile information. After logging in with Facebook, I use "uid" to check the firebase database, if the user exists, and retrieves some of the information stored in the database.
The problem is right now if, because the request to read the isync database, I canβt get the update information in the view, and using $scope.$apply() only works until I change the pages as soon as I click on a page using another controller, I get the following:
Error: [$rootScope:inprog] http://errors.angularjs.org/1.3.15/$rootScope/inprog?p0=%24digest
controller
app.controller("profile", function($scope, loginService, Auth, userManagement) { var authData = Auth.$getAuth(); if (authData != null) { $scope.authData = authData; userManagement.saveLastLogin(authData); userManagement.userDatabaseRead(authData).done(function(userDatabase) { $scope.userDatabase = userDatabase; $scope.$apply(); }); }; $scope.logoutFB = function() { loginService.logoutUser(); Auth.$onAuth(function(authData) { $scope.authData = authData; }); }; })
Factory
app.factory("userManagement",function(FirebaseUrl) { return { saveLastLogin: function(authData) { var userId = authData.uid; var ref = new Firebase(FirebaseUrl); var users = ref.child("users"); var timestamp = Date.now(); users.child(authData.uid).update({ last_activity: timestamp, }); console.log('user ' + userId + ' last login was on' + timestamp); }, userDatabaseRead: function(authData) { var ref = new Firebase(FirebaseUrl+"users/"+authData.uid); var data, def = $.Deferred(); ref.on("value", function(snapshot) { var userDatabase = snapshot.val(); def.resolve(userDatabase); }, function (errorObject) { console.log("The read failed: " + errorObject.code); }); return def.promise(); }, } });
Update 1: Preview
This is my opinion, what I'm trying to do is when the user is logged in, show some information from the firebase structure belonging to this user uid
<div class="jumbotron"> <h1>Profile View</h1> <p ng-show="authData">Welcome {{ authData.facebook.cachedUserProfile.first_name }}</p> <p ng-show="authData">{{userDatabase.last_activity | date:'yyyy-MM-dd HH:mm:ss Z'}}</p> <button ng-click="logoutFB()" ng-show="authData" class="btn btn-danger facebook-login">Logout</button> </div>