I'm trying to check if a user has a specific role from my component
's template
.
For example, this could be my session service :
function Session() {
'ngInject';
var userRoles = {};
...
function hasRole(role) {
if(userRoles[role]) {
return true;
}
else {
return false;
}
};
return {
getUserRoles: getUserRoles,
setUserRoles: setUserRoles,
hasRole: hasRole
};
};
I'll then assin my service to the $rootScope in my .run()
method :
$rootScope.session = Session;
But it seems that I cannot access my session service from my component's template :
<div ng-show="session.hasRole('TEST')">
Hi, {{session.getUserRoles() | json}}
</div>
It only works when I specify the $root service :
<div ng-show="$root.session.hasRole('TEST')">
Hi, {{$root.session.getUserRoles() | json}}
</div>
Why is that ? And how can I avoid using $root
, to access my $rootScope
's services when using angular's component
architecture ?