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

旧街凉风 提交于 2019-12-20 12:26:26

问题


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.


回答1:


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.




回答2:


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')




回答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'



回答4:


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




回答5:


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

$location.absUrl().substr(0, $location.absUrl().lastIndexOf("#")) + "/getusers"



回答6:


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,
            });
};



回答7:


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());

....

}]);


来源:https://stackoverflow.com/questions/17022285/whats-the-better-way-to-identify-the-context-path-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!