2017-04-18 8 views
0

저는 Firebase를 사용하고 index.html의 측면 탐색 막대를 사용하여 AngularJS에서 단일 페이지 응용 프로그램을 만들고 있습니다.

그리고 로그인 후 사용자 이름이 측면 탐색 막대에 있어야하지만 컨트롤러의 현재 페이지에서 $ 범위가 작동하고 싶습니다. 그래서 당신의 mainController 당신이 인증 변경을 청취 할 필요가 없습니다에 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"; 
    });