In order to change the login button to display "logged in as xxx" after authentication, I have structured my page into three views: header, content, footer. The login button is located in the header view. Upon clicking login, it transitions to the "app.login" state, allowing the content view to update for user input of username and password.
Below is the routing code snippet:
app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/',
views: {
'header': {
templateUrl: 'static/templates/header.html',
controller: 'AppController'
},
'content': {
templateUrl: 'static/templates/home.html',
controller: 'HomeController'
},
'footer': {
templateUrl: 'static/templates/footer.html',
}
}
})
.state('app.login', {
url: 'login',
views: {
'content@': {
templateUrl : 'static/templates/login.html',
controller : 'LoginController'
}
}
})
The HTML template includes the following code snippet:
<li><span ng-if='loggedIn' class="navbar-text">
Signed in as {{currentUser.username}}</span>
</li>
The LoginController sets a $scope.loggedIn
flag to true upon successful authentication, but the challenge lies in populating that flag to the header view. While utilizing $scope.loggedIn
directly in the HTML template poses scope issues between controllers, leveraging $scope.$emit
and $scope.$on
within parent-child controllers might not be feasible due to the separation of views.
Although resorting to $rootScope is an option, best practices discourage polluting the $rootScope unnecessarily. Given the commonality of this use case, there must be a simple solution that I am overlooking.