我如何显示/隐藏按钮在角根据用户授权
本文关键字:按钮 授权 用户 隐藏 何显示 显示 | 更新日期: 2023-09-27 17:54:07
我试过这样做
<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
最好使用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>