0

I have a ng-repeat for article comments, that looks like this:

<div ng-repeat="comment in comments">
          <li class="item" ng-class-even="'even'">
            <div class="row">
              <div class="col">
                <i class="icon ion-person"></i> {{ comment.user.first_name }} {{ comment.user.last_name }}
                <i class="icon ion-record"></i> {{ comment.created_at }}
              </div>
              <!-- TODO: this needs to be an ng-if admin -->
              <div ng-show="hasRole(comment.user)" class="col right">
                <i class="icon ion-record admin"></i> Admin
              </div>
            </div>
            <div class="row">
              <div class="col">
                <p>{{ comment.text }}</p>
              </div>
            </div>
          </li>
        </div>

I am trying to show this part only if the user is an admin:

<div ng-show="hasRole(comment.user)" class="col right">
                <i class="icon ion-record admin"></i> Admin
              </div>

I have tried to set that up following the answers here.

So I made a function in my controller:

 $scope.hasRole = function(roleName) {
    return $scope.comments.user.roles.indexOf(roleName) >= 0;
  }

But it returns -1 every time, even when the user is an admin. My data looks like this:

1:Object
    $$hashKey: "object:28"
    article_id:"2"
    created_at:"2016-05-12 12:19:05"
    id:6
    text:"someCommentText"
    updated_at:null
    user:Object
        active:"1"
        created_at:null
        first_name:"admin"
        id:1
        last_name:"admin"
        roles:Array[1]
            0:Object
            created_at:null
            id:1
            name:"Admin"
            parent_id:null
            pivot:Object
            slug:"admin"

3 Answers 3

2

Use this in your HTML

<div ng-show="hasAdminRole(comment.user.roles)" class="col right">
    <i class="icon ion-record admin"></i> Admin
</div>

this is the method to determine that the user belongs to the admin role or not.

$scope.hasAdminRole = function(roles) {
    var isAdmin = false;
    for(var i = 0; i < roles.length; i++) {
        if (roles[i].name == 'Admin') {
            isAdmin = true;
            break;
        }
    }

    return isAdmin;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you have an error on this line?

var indexOfRole = $scope.comments.indexOf(user.roles);

You are looking here to see if the list of roles for this users exists within the array of comments.

Maybe you need to just check in the actual user.roles array and see if there is an Admin role there? Something like:

$scope.hasRole = function(user) {
  for (var i = 0; i < user.roles.length; i++) {
    if (user.roles[i].slug === 'admin') { return true; }
  }
  return false
}

Comments

0

That's because it's an object, you can fetch the index of only array. In the link that you provided is an array.

Comments

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.