我如何根据用户授权以角度显示/隐藏按钮?

时间:2016-07-03 10:55:09

标签: javascript c# asp.net angularjs authorization

我尝试过这样的事情

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

在我的javascript中

  $scope.isAuthenticated = function() {
                $http.get("api/user/getAuthenticatedUser")
                    .success(function(user) {
                        if (user != null) {
                            return true;
                        }
                        return false;
                    });
            }

但它在$ rootscope上给我一些错误

1 个答案:

答案 0 :(得分:0)

最好使用angular提供的$ http服务。将身份验证变量设置为false,从后端调用身份验证服务并更改身份验证变量的值。从Angular绑定会将值传输到视图,您将能够在视图中使用该值。

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

// make the method that checks autentication
$scope.checkAuth = function() {
    $http({
      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
$scope.checkAuth();

HTML:

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