Whats the better way to identify the context path in Angular JS
Asked Answered
H

7

12

I have my web application deployed to tomcat with an applicatio context. For example my URL looks something like this.

http://localhost:8080/myapp

myapp - is the application context here.

Now in an Angular service if i want to call a webservice say getusers. My URL should be this /myapp/getusers. But I want to avoid hardcoding the application context as it might change from one deployment to other. I have managed to figureout the contextpath from $window.location.pathname but it looks very stupid. Is there a betterway?

FYI I am using Spring MVC for restful services.

Hoenack answered 10/6, 2013 at 10:57 Comment(0)
B
12

What I have done is declared a variable in the main jsp file. Then that variable will be available throughout the angular application.

<script type="text/javascript">
    var _contextPath = "${pageContext.request.contextPath}";
</script>

This code should be written in header before including other JavaScript libraries.

Buskus answered 2/9, 2013 at 14:54 Comment(1)
How to access this variable in an Angular Service ?Yehudit
L
7

I'm also using tomcat and Spring MVC. Using relative url in JavaScript will do the trick.

For doing this you just need to remove the / at the begining of REST url. so that your url starts from the current url in your browser.

replace $resource('/getusers') with $resource('getusers')

Lyublin answered 16/6, 2015 at 12:14 Comment(0)
P
3

Inject the $location service to your controller.

 var path = $location.path(); // will tell you the current path
     path = path.substr(1).split('/'); // you still have to split to get the application context

 // path() is also a setter
 $location.path(path[0] + '/getusers');
 // $location.path() === '/myapp/getusers'

 // ------------------ \\

 // Shorter way
 $location.path($location.path() + '/getusers');
 // $location.path() === '/myapp/getusers'
Potiche answered 10/6, 2013 at 15:21 Comment(1)
If you are using angular in a non-single page application, this method is not reliable. e.g. calling ${contextPath}/api/delegation/apps. The method above has no idea if the application has been redeployed to the root context or pages are called from various nested paths.Punchy
B
2

In Angular 2 (if using hashbang mode). Below code can be used to form the url.

document.location.href.substr(0, document.location.href.lastIndexOf("/#")) + "/getusers";

Inspired from the answer of @jarek-krochmalski

Beeler answered 28/12, 2017 at 18:5 Comment(0)
E
1

if you are using hashbang mode, with "#", you can do something like that:

$location.absUrl().substr(0, $location.absUrl().lastIndexOf("#")) + "/getusers"
Esme answered 10/12, 2013 at 10:0 Comment(0)
S
1

For AngularJS $http service you are good to go with url : 'getusers', as follows:

$scope.postCall = function(obj) {
            $http({
                method : 'POST',
                url : 'getusers',
                dataType : 'json',
                headers : {
                    'Content-Type' : 'application/json'
                },
                data : obj,
            });
};
Satem answered 17/3, 2017 at 10:41 Comment(0)
G
0

In general, you should use injection in your controller like the following:

angular.module("yourModule").controller("yourController", ["$scope", "yourService", "$location", function($scope, yourService, $location){

....
      //here you can send the path value to your model.

      yourService.setPath($location.path());

....

}]);
Grayson answered 20/8, 2017 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.