Guest User

Untitled

a guest
Jun 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. angular.module(...)
  2. .service('RolesSerivce', function() {
  3. this.hasRole = function(roleName) {
  4. return this.roles.indexOf(roleName)>1;
  5. };
  6. this.setRoles = function(rolesArr) {
  7. this.roles = rolesArr;
  8. };
  9. this.setRoles(['ROLE_CAN_CLICK'])
  10. }).direcive('hasRole', function() {
  11. ??????????????????
  12. // use RolesSerivce.hasRole inside implemetation
  13. ??????????????????
  14. });
  15.  
  16. <button has-role="ROLE_CAN_CLICK" ng-disabled="extraValidation > 0">Click me</button>
  17.  
  18. angular.module(...)
  19. .service('RolesService', function() {
  20. this.hasRole = function(roleName) {
  21. return this.roles.indexOf(roleName)>1;
  22. };
  23. this.setRoles = function(rolesArr) {
  24. this.roles = rolesArr;
  25. };
  26. this.setRoles(['ROLE_CAN_CLICK'])
  27. }).directive('hasRole', function(RolesService) {
  28. return {
  29. link: function (scope, element, attrs) {
  30. $(element).removeAttr('ng-disabled');
  31. if(RoleService.hasRole(attrs.hasRole)) {
  32. $(element).removeAttr('disabled');
  33. } else {
  34. $(element).attr('disabled', 'disabled');
  35. }
  36. }
  37. };
  38. });
  39.  
  40. (extraValidation > 0)==true (extraValidation > 0)==false
  41.  
  42. ROLE_CAN_CLICK==true DISABLED ENABLED
  43.  
  44. ROLE_CAN_CLICK==false DISABLED DISABLED
  45.  
  46. <button ng-disabled="(!ROLE_CAN_CLICK) || (extraValidation > 0)">
  47. Click me
  48. </button>
Add Comment
Please, Sign In to add comment