how to enable disable elements based on user role?
Closed this issue ยท 11 comments
Hey I am hiding and showing the elements using angular permission. Now how to enable or disable the elements based on permisssion?
This is giving me error 'permission.onUnauthorized(...) is not a function'.
Same for me. Any updates?
I worked this around by setting PermissionStrategies to the current scope:
$scope.PermissionStrategies = PermissionStrategies;
Make sure to inject PermissionStrategies.
@motymichaely great, works perfectly! thank you, bro!
Hi can someone please show an example of how to write this permission to disable based on the permission setting? Thanks
@thiagomcdc please help me with this problem. Thanks in advance
@thari911 you can use this as a sample code snippet:
Put this in your controller (make sure to inject PermPermissionStrategies
):
var permissionStrategies = angular.extend({}, {
enableElement: function($element) {
PermPermissionStrategies.enableElement($element);
$element.addClass('authorized');
},
disableElement: function($element) {
PermPermissionStrategies.disableElement($element);
// Some custom code for the element
$element.addClass('unauthorized');
},
showElement: function($element) {
PermPermissionStrategies.showElement($element);
},
hideElement: function($element) {
PermPermissionStrategies.hideElement($element);
}
});
$rootScope.permissionStrategies = permissionStrategies;
The following in your views as an example to enable or disable an anchor based on ui-router permissions:
<a ui-sref="some.router.path"
permission
permission-sref="'some.router.path'"
permission-on-authorized="$root.permissionStrategies.enableElement"
permission-on-unauthorized="$root.permissionStrategies.disableElement">
This will be disabled with no permission to access 'some.router.path'.
</a>
@masterspambot could you please assist me with this? anchors are not been disabled. just buttons and inputs only seem to get disabled