Firebase 로그인 성공 후 ng-show/hide 문제가 발생했습니다. 나는 다음과 같이 $ rootScope에서 loggedIn 변수가 :성공적인 Firebase 로그인 후 ng-show/hide가 작동하지 않습니다.
app.run(function($rootScope){
$rootScope.loggedIn = false;
console.log("loggedIn", $rootScope.loggedIn);
});
을 그리고처럼 내 로그인 컨트롤러에서 $ rootScope을 주입 한 : 나는이 개 로그인 기능을 테스트 한 장소에두고
app.controller("loginCtrl", ['$scope', '$rootScope', '$firebaseObject', function($scope, $rootScope, $firebaseObject){....
. 첫 번째 파일은 더미 파일이며 실제로 Firebase 인증 프로세스를 거치지 않지만 뷰의 관련 요소를 표시하거나 숨 깁니다.
두 번째는 Firebase auth
을 통과하며 첫 번째 더미 함수와 정확히 동일하게 $rootScope.loggedIn = true
을 기록합니다. 그러나 show/hide
이 작동하지 않습니다. 여기
// This works
$scope.logIn = function(){
$rootScope.loggedIn = true;
console.log("loggedIn", $rootScope.loggedIn);
}
// This doesn't work
// logs into Firebase fine but ng-show/hide doesn't work
$scope.logIn = function(){
ref.authWithPassword({
email : $scope.user.email,
password : $scope.user.password
},
function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Authenticated successfully with payload:", authData);
// NEED TO FIX!!!
$rootScope.loggedIn = true;
console.log("loggedIn", $rootScope.loggedIn);
}
});
}
는 HTML에서 NG-표시/숨기기
<div class="login" ng-hide="loggedIn" ng-controller="loginCtrl">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" ng-model="user.email" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" ng-model="user.password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" ng-click="logIn()">Sign in</button>
</form>
</div><!-- ./login form -->
<!-- main page container -->
<div class="container" ng-controller="myCtrl">
<div class="main-content" ng-show="loggedIn">
<h1>{{ pageTitle }}</h1>
<hr>
<div id="successNotice" class="alert alert-success" role="alert" ng-show="savedSuccess">
Musician saved successfully!
</div>
<table class="table table-striped">
<thead>
<tr>
<label>Enter the name of your favourite musician</label>
<br>
<th>
<input class="form-control" placeholder="First Name" ng-model="dev.firstName" name="firstName">
</th>
<th>
<input class="form-control" placeholder="Last Name" ng-model="dev.lastName" name="lastName">
</th>
<th>
<button class="btn btn-success pull-right" >
<span class="glyphicon glyphicon-plus" ng-click="add(dev)"></span>
</button>
</th>
</tr>
</thead>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="musician in musicians">
<td>{{ musician.firstName }}</td>
<td>{{ musician.lastName }}</td>
<td>
<button class="btn btn-danger pull-right" ng-click="remove($index)">
<span class="glyphicon glyphicon-remove"></span>
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div><!-- ./main-content -->
나는 아마이 작업을 수행하는 가장 또는 가장 안전한 방법은 아닙니다 알고 더 분리 및 공상 라우팅가는이 있어야한다입니다 에. 하지만 더 나은 각도 기법을 사용하여 비즈니스 로직을 리팩토링하기 전에 개념 증명으로 테스트 해보고 싶었습니다. 잠시 동안 왜 이것이 작동하지 않는지 알고 싶습니다. 감사!
불행히도 그건 효과가 없었습니다. 어쨌든'$ rootScope'는 뷰 내의 코드에 접근 할 수 있습니다. 왜냐하면 그것은 컨트롤러 내의 새로운'$ scope' 변수에 저장 될 필요가없는 코드 내'login' 함수의 매력처럼 작동하기 때문입니다. 내가 틀렸다면 나를 바로 잡아라. 그러나'$ rootScope'가 무엇을위한 것이 아닌가? 어느 뷰나 컨트롤러가 사용되는지에 관계없이 앱의 어느 곳에서나 전역 상태를 캡처 할 수 있습니까? 응답 주셔서 감사합니다! – mikeym