Answer the question
In order to leave comments, you need to log in
How to check if a user is logged in angular?
Good afternoon.
I want to check whether the user is authorized in the template and, based on this, show or hide navigation elements. The template is like this:
<ul class="nav nav-tabs" ng-controller="AuthController">
{% verbatim %}
<li ng-class="getClass('/')"><a href="#/">Главная</a></li>
<li ng-hide="auth" ng-class="getClass('/login')"><a href="#/login">Войти</a></li>
<li ng-show="auth" ng-class="getClass('/logout')"><a href="#/logout" ng-click="logout()">Выйти</a></li>
<li ><a href="">{{ auth }}</a></li>
{% endverbatim %}
</ul>
function AuthController($scope, $location, $http){
$scope.logout = function() {
$http.get('api/auth/logout/').success(function(){
$scope.auth = false;
$location.path('/login');
});
};
$scope.login = function() {
$http({
method: 'POST',
url: 'api/auth/login/',
data: $scope.user
})
.success(function(data){
$scope.auth = true;
$location.path('/');
});
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question