Search code examples
javascriptangularjsauthenticationangular-ng-if

How can I use ng-if to display a button based on the current user?


I am create a user profile and would like to show an edit button that only displays for the profile's owner. I am attempting to use ng-if so that the edit capabilities are removed from the DOM.

How can I use ng-if to display a button based on the current user?

In my view:

<button ng-if="editProfile()">EDIT</button>

In my controller:

$scope.editProfile = function (canedit) {
  var canedit = false;
  for (var i=0;i<$scope.users.length;i++) {
    if ($scope.users[i].id===$scope.currentUser.id) {
      canedit = true;
     }
  }
}

Solution

  • Your function needs to return true or false

    <button ng-if="editProfile()">EDIT</button>
    
    
    $scope.editProfile = function () {
      for (var i=0;i<$scope.users.length;i++) {
        if ($scope.users[i].id===$scope.currentUser.id) {
          return true
         }
      }
      return false
    }