Juan BC
Juan BC

Reputation: 196

How to use request parameters from Spring MVC in AngularJS

I am a beginner with AngularJS and Im facing the following problem:

I have a screen with a side-nav that every link is managed by AngularJS' router. The point is that some options from the side-nav as well as some content will be only displayed to some special kind of users, lets called them Admins.

I want to use something like:

<ng-if="isAdmin == true">
  ....
</ng-if>

so the question is how I set isAdmin variable with a value that is coming from the request?

Upvotes: 0

Views: 522

Answers (1)

Jeroen Noten
Jeroen Noten

Reputation: 3624

I am not familiar with Spring, but you can for example make an Angular $http request to Spring to ask if the user is an admin, and let it send back true or false, and set the result of the response in the controller. Like this:

var url = 'someUrl'; // URL to the spring framework that responds with a JSON type
                     // response containing { isAdmin: true } or { isAdmin: false }
$http.get(url).success(function(response) {
    $scope.isAdmin = response.data.isAdmin;
});

Edit:

Alternatively, you can set a global variable in JavaScript, for example somewhere in <head> and put it on the $rootScope. I don't know exactly how to do this with Spring, but the HTML must look something like this:

<script>
var isAdmin = true; // or false
</script>

Then, you have access to this variable everywhere, so you can put it on the $rootScope in your main controller:

$rootScope.isAdmin = isAdmin;

Then, you have access to it in all views.

Upvotes: 1

Related Questions