Looking to determine a user's status in my AngularJS app in order to display specific functionality content, here is the HTML code in my view:
<span ng-show="authService.isSuperUser()">You are a Super User</span>
To check if the user has "superuser" status and show the span, I wrote the following JavaScript code within my service:
this.isSuperUser = function () {
if (loggedInUser !== null) { // loggedInUser is true in most cases
return getSuperUser();
} else {
return false;
}
};
var getSuperUser = function(){
return $http({method: 'GET', url: '/url/to/rest/service/for/superuser' }).then(function (response) {
return response; // this returns true or false
}, function () {
console.log('Yikes! An Error!');
return false;
});
};
The getSuperUser
function gives the correct result (either true or false), but when testing it in the browser, I encounter numerous Infinite $digest Loop errors. It seems like there's a better approach for implementation to avoid these errors. The HTML view loads when a user visits a specific URL such as myapp.com/#/youraccount, and the
ng-show="authService.isSuperUser()"
is not loaded prior in the app.