Chuck Villavicencio
Chuck Villavicencio

Reputation: 393

Show/Hide buttons if i don't have access

I have an api which identify if i have any permissions. What i want to do, is to show/hide a button if i don't have an specific permission.

This is the code where i'm consume the api:

function users (){
        $http.get('/api/users')
        .then(function(data){
            $scope.thisUser = data.data.Response;
            //console.log($scope.thisUser);
        });
    }

It response something like this:

0:{RoleName: "basic user", Select: true}
1:{RoleName: "advance user", Select: false}

How can i pass on an ng-show/ng-hide the params to allow or deny the visibility of the next button, if, per example, i don't have the advance user permission (if Select: false):

<button class="btn btn-info">Next Step</button>

I don't trigger any search for the permission, because when i'm consuming the api, it identifies the permission of the user.

I'm using AngularJs and Javascript.

Thanx in advance.

Upvotes: 0

Views: 690

Answers (2)

holyfire
holyfire

Reputation: 21

If you are able to use the library called lodash, you should be able to do

<button ng-show="isUserAdvanced()" class="btn btn-info">Next Step</button>

And then add a new method as follows:

    isUserAdvanced() {
        return _.findIndexOf( $scope.thisUser, {RoleName: "advanced user", Select: true}) != -1;
  }

As @skyboyer mentioned, it is probably better do to this without the lodash dependency:

isUserAdvanced() {
            return $scope.thisUser.some(role => role.Select && role.RoleName == "advanced user");
      }

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222582

You could use find or some

isUserAdvanced() {
     return  $scope.thisUser.some(role => role.Select)
} 

Upvotes: 1

Related Questions