Best way to create an isLoggedIn function in AngularJS that can be accessed in any controller or template?

I need the function isLoggedIn (), which all controllers and templates have access to (for which templates are needed to do something like ng-show = isLoggedIn () "). What is the best way to do this?

If the answer is a service, is it possible to access the service from a template, or should each of my controllers create a wrapper function to view the template (in $ scope)?

+7
source share
1 answer

Usually I have "MainCtrl" in the body tag and put global things in it.

<body ng-controller="MainCtrl"> ... </body> function MainCtrl($scope, authService) { $scope.isLoggedIn = function() { return authService.isLoggedIn(); } } 

Then each other region inherits the isLoggedIn function.

You can also put isLoggedIn in $ rootScope, but I like it.

+5
source

All Articles