0

I have a list of buttons like this

<div style="position: absolute; top:0; right: 0;">
 <button class="nav-link-btn" [routerLink]="['/dashboard']"> Dashboard </button>
 <button class="nav-link-btn" [routerLink]="['/admin/templates-dashboard']"  > Templates </button>
 <button class="nav-link-btn" [routerLink]="['/admin/roles-dashboard']" > Users </button>
 <button class="nav-link-btn" [routerLink]="['/corporate/dashboard']"> Corporates </button>
 <button class="nav-link-btn" [routerLink]="['/archive']"> Archive </button>
 <button id="name-btn" class="nav-link-btn" [routerLink]="['/my-profile']"  >
        <span id="circle"></span> {{ name }}
 </button>
 <button  id="logout-btn" (click)="isDialogOpen = true" class="main">Log out</button>
</div>

I then have a user role that is stored in local storage. I am now trying to disable/enable the above buttons based on the users role.

eg if the userRole is a system admin then disable the Archive button but if the userRole is HR Admin then enable the Archive button

I am trying the following but it does not seem to be working as expected

isDisabled(userRole: string): boolean {
    if (userRole == 'System Admin') {
      return true;
    } else {
      return false;
    }
 }

[disabled]="isDisabled(userRole)"

5
  • Where is userRole from? Commented May 13, 2019 at 6:12
  • userRole is stored in localStorage Commented May 13, 2019 at 6:13
  • Which button are you trying to disable? Commented May 13, 2019 at 6:14
  • All the buttons will be enabled/disabled based on the user role. For now I am trying to disable the archive button if the user is System Admin but it must be enabled if it is a HR Admin Commented May 13, 2019 at 6:15
  • Possible duplicate of Angular2 Forms - Submit Button Disabled? Commented May 13, 2019 at 6:17

1 Answer 1

1

You should still make use of the input bindings and bind the disabled attribute of all buttons to the isDisabled() method. You won't be able to access userRole on your template (component.html) unless you have set it as a property.

<button [disabled]="isDisabled()" class="nav-link-btn" [routerLink]="['/archive']"> Archive </button>

And on your component.ts, you can get the userRole within the isDisabled() method. I am assuming the key is called userRole as well.

isDisabled(): boolean {
    var userRole = storage.getItem(`userRole`);
    if (userRole == 'System Admin') {
      return true;
    } else {
      return false;
    }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Do I then need to make one function per button to specify which user role has access to which button?
@skydev yes, you need to include` [disabled]="isDisabled()"` on all buttons that require that perticular function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.