I am writing an Angular application where the navbar is supposed to be different depending on whether or not the user is logged in. I have an angular view that looks like this:
<!-- Auth -->
<nav ng-if="isAuthenticated()" class="navbar navbar-default" role="navigation">
<div class="navbar-header">
...
</div>
</nav>
<!-- No auth -->
<nav ng-if="!isAuthenticated()" class="navbar navbar-default" role="navigation">
<div class="navbar-header">
...
</div>
</nav>
<!-- Content -->
<div ng-view></div>
With this controller:
$scope.isAuthenticated = function() {
return $auth.isAuthenticated();
};
When the page initially loads, this works perfectly; however, after the user logs in, the navigation view doesn't change, even though isAuthenticated() now returns a different value. I read about ng-if creating it's own scope, and tried to do this:
<!-- Auth -->
<nav ng-if="nav.auth" class="navbar navbar-default" role="navigation">
<div class="navbar-header">
...
</div>
</nav>
<!-- No auth -->
<nav ng-if="!nav.auth" class="navbar navbar-default" role="navigation">
<div class="navbar-header">
...
</div>
</nav>
<!-- Content -->
<div ng-view></div>
Controller:
$scope.nav = {
auth: false
};
$scope.isAuthenticated = function() {
return $auth.isAuthenticated();
};
$scope.$watch($scope.isAuthenticated(), function(value) {
console.log("Authentication changed; status: " + value);
$scope.nav.auth = value;
});
But this doesn't seem to work either. Is there a way I can update ng-if so that the isAuthenticated
function is checked continuously?