Ioan Adrian Duliba Ioan Adrian Duliba - 1 year ago 67
Javascript Question

How i show/hide a button in angular according to user authorization?

I have tried something like this

<button ng-if="!isAuthenticated()" ng-click="deleteReview()">Delete</button>

And in my javascript

$scope.isAuthenticated = function() {
.success(function(user) {
if (user != null) {
return true;
return false;

But it return me some errors at $ rootscope

Answer Source

Better use $http service provided by angular. Set authentication variable as false, call the authentication service from backend and change the value of authentication variable. Binding from Angular will transmit the value to the view and you will be able to use the value in view.

   $scope.isAuthenticated = false; // init as false

// make the method that checks autentication
$scope.checkAuth = function() {
      method: 'GET',
      url: 'api/user/getAuthenticatedUser'
    }).then(function successCallback(user) {
        if (user != null) {
            $scope.isAuthenticated = true;
        } else {
            $scope.isAuthenticated = false;

      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.


// call the autentication method


<button ng-if="!isAuthenticated" ng-click="deleteReview()">Delete</button>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download