I have tried something like this
JavaScript
x
2
1
<button ng-if="!isAuthenticated()" ng-click="deleteReview()">Delete</button>
2
And in my javascript
JavaScript
1
10
10
1
$scope.isAuthenticated = function() {
2
$http.get("api/user/getAuthenticatedUser")
3
.success(function(user) {
4
if (user != null) {
5
return true;
6
}
7
return false;
8
});
9
}
10
But it return me some errors at $ rootscope
Advertisement
Answer
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.
JavaScript
1
25
25
1
$scope.isAuthenticated = false; // init as false
2
3
// make the method that checks autentication
4
$scope.checkAuth = function() {
5
$http({
6
method: 'GET',
7
url: 'api/user/getAuthenticatedUser'
8
}).then(function successCallback(user) {
9
if (user != null) {
10
$scope.isAuthenticated = true;
11
} else {
12
$scope.isAuthenticated = false;
13
}
14
15
}, function errorCallback(response) {
16
// called asynchronously if an error occurs
17
// or server returns response with an error status.
18
});
19
20
}
21
22
23
// call the autentication method
24
$scope.checkAuth();
25
HTML:
JavaScript
1
2
1
<button ng-if="!isAuthenticated" ng-click="deleteReview()">Delete</button>
2