2014-12-02 38 views
1

嘿傢伙,所以我使用firebase和angular來構建示例應用程序。爲什麼我需要刷新頁面來查看ng-show的新值

這是註冊功能

$scope.register = function(){ 
    Authentication.register($scope.user) 
     .then(function(user){ 
      Authentication.login($scope.user);              
     }) 
     .then(function(user){ 
      $timeout(function(){ 
       $location.path('/meetings'); 
      }, 5); 
     }) 
     .catch(function(error){ 
      $scope.message = error.toString(); 
     }); 


} //register 

此功能在這家工廠

myApp.factory('Authentication', function($firebase, 
       $firebaseAuth,$location, FIREBASE_URL){ 

    var ref = new Firebase (FIREBASE_URL); 
    var auth = $firebaseAuth(ref);  

    var myObject = { 
     login : function (user){ 
       return auth.$authWithPassword({ 
        email: user.email, 
        password: user.password 
       }); 
     }, // login 

     logout: function(){ 
      return auth.$unauth(); 
     }, 
     register : function (user){ 
      return auth.$createUser(user.email,user.password); 
     } //register 
    } //myObject 
    return myObject; 
}); 

這裏調用方法是status.js控制器根據用戶是否登錄而改變我的索引或不是

auth.$onAuth(function(authData){ 
     if (authData){ 
      console.log("i entered authData"); 
      $scope.userStatus = authData.password.email; 

     } else { 

      $scope.userStatus = false; 
     } 
    }); 

index.html文件的一部分

<div class="userinfo" 
ng-controller="StatusController" ng-show="userStatus"> 
    <span class="user">Hi {{userStatus}}</span> 
    <a href="#" ng-click="logout()">Log Out</a> 
</div> 

我的問題是,NG-視圖需要刷新頁面,顯示新價值。它不會自動顯示它,但我的代碼工作。如果我刷新頁面手動我可以看到,用戶得到了註冊並登錄。

約2小時,現在和$範圍搜索。$應用在這裏似乎NT是這種情況。

預先感謝您

+0

不知道答案,但文檔是指'onAuth',而不是'$ onAuth',會不會是? – 2014-12-02 16:50:41

+0

嘗試將ng-show與控制器內聯。 – geckob 2014-12-02 16:50:44

+0

爲什麼奇怪的'myObject'包裝什麼都不做?考慮使用'return $ firebaseAuth(ref);' – Kato 2014-12-04 20:47:39

回答

0

我要感謝你們爲可能的解決方案,但它似乎是解決之道在於$超時變量。

由於下面,我從本地環境中使用GulpJS有我的劇本和火力地堡API談之間的延遲工作的API會談的註冊功能。

$scope.register = function(){ 
Authentication.register($scope.user) 
    .then(function(user){ 
     Authentication.login($scope.user);              
    }) 
    .then(function(user){ 
     $timeout(function(){ 
      $location.path('/meetings'); 
     }, 5); 
    }) 
    .catch(function(error){ 
     $scope.message = error.toString(); 
    }); 


} //register 

這就是爲什麼我把$超時角功能在那裏擺在首位,但我忘了,在$超時功能輸入數字5毫秒,它似乎因爲我的設置(使用本地環境,同時在線與Firebase API交談),我發現如果我在重定向中使用2秒的延遲,則會自動顯示更改,而無需手動刷新頁面。

正確的代碼會是下面這樣:

$scope.register = function(){ 
    Authentication.register($scope.user) 
     .then(function(user){ 
      Authentication.login($scope.user);              
     }) 
     .then(function(user){ 
      $timeout(function(){ 
       $location.path('/meetings'); 
      }, 2000); 
     }) 
     .catch(function(error){ 
      $scope.message = error.toString(); 
     }); 
} //register 

我懷疑,如果我用我的角度應用程序在網絡上我可能降低延遲,甚至完全刪除$超時功能。