2017-04-18 56 views
0

我在使用Firebase的AngularJS中使用index.html上的侧面导航栏制作单页面应用程序。

登录后,我希望用户名应该在侧面导航栏上,但$ scope正在为控制器上的当前页面工作。为使您的mainController你不需要监听AUTH变化在docs描述

scotchApp.controller('mainController', function($scope) { 
    $scope.validateLogin = function() 
    { 
     var email = $scope.login.userName + "@xyz.co"; 
     var password = $scope.login.password; 
     firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { 
      $scope.message = "please enter the correct details"; 
     }); 
     firebase.auth().onAuthStateChanged(function(user) 
     { 
      if(user) 
      { 
       $scope.usermobile = $scope.login.userName; 
       window.location.assign("/#/equipment"); 
      } 
     }); 
    } 
    $scope.message = ''; 
}); 
+0

我不明白这篇文章:“但$范围正在为控制器上的当前页面工作。” - 你能澄清一下吗? –

+0

为什么不使用'window.location.hash =“/#/ equipment”; ' –

+0

@CliveSeebregts 实际上$ scope适用于当前路由页面 ,但是我想使用那个路由控制器 –

回答

0

当前用户是firebase.auth().currentUser可用。

在代码中的每个用户试图登录时要连接一个新的监听器,但你真正想要的是一个成功登录响应,所以只需添加一个then您登录电话:

firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(function (user) { 
    console.log('the logged in user: ', user); 
    // Go to the other route here. 
    // It is recommended to use the router instead of "manually" changing `window.location` 
    }) 
    .catch(function(error) { 
    $scope.message = "please enter the correct details"; 
    });