Within all of my controllers, I currently have the following code that is executed when a link is clicked in the view:
$scope.logout = function() {
authenticationService.logout();
$scope.isAuthUser = false;
delete $localStorage.selectedModel;
$location.path('/login').search('key', null);
};
As a newcomer to angularJS, I am curious about the best approach:
- Should I place this logic in a service?
Or
- Is it better to utilize a directive?
When using a directive, I understand how to use controllers like $scope.isAuth = false;
, but with a service, I am unsure how to implement it without repeating the code in every controller as shown below:
$scope.logout = function() {
authenticationService.logout();
myNewService.logout();
$scope.isAuthUser = false;
};
In the view:
<a href="javascript:void(0)" data-ng-click="logout()">
<span>Sign Out</span>
</a>
I am concerned that this approach may not be optimal. What is the recommended way to handle this situation?