There are three components we need to get communicating; you already know there's the controller where the ngSwitch takes place and there's the API service. The third is some type of user state service that you use to manage user info. This type of service is very common in AngularJS apps. See this answer for an idea of what that service might look like.
Your API service would, on 401, tell that service that we are currently logged out. You could also use a generic $http
interceptor for this, but it would affect all $http
calls. Your choice here.
Your controller then only has to watch for this current state:
$scope.$watch( AuthService.isLoggedIn, function ( isLoggedIn ) {
$scope.isLoggedIn = isLoggedIn;
});
And your template can use ngShow on that scope variable:
<div class="login" ng-show="!isLoggedIn">
...
</div>
This service is also available to any controller that needs it. Your API service can even check the value before issuing any requests to prevent any unnecessary API calls if we have a priori knowledge the user isn't logged in.
There are countless variations on this that depend on your app's structure and your existing code, but hopefully this will set you on the right path.