Whats the better way to identify the context path in Angular JS

darwinbaisa picture darwinbaisa · Jun 10, 2013 · Viewed 25k times · Source

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.

Answer

Omer Arshad picture Omer Arshad · Sep 2, 2013

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.