角JS:如何设置上下文路径通过HTTP $所有呼叫? [英] Angular JS: How to set context path for all calls through $http?
问题描述
我在一个Web服务器,其中应用可能住在 HTTP部署我的角度应用$ C> http://foobars.com/
,或一些其他网址。我没有关于绝对路径任何保证 - 有时通话可能需要到 /应用程序名称/富
,有时 / foo的
,有时 / foobars /富
。换句话说,有一个运动部件: /应用程序名
,(空)
, / foobars
在上述样品。也称为上下文路径。
I'm deploying my Angular application in a web server, where the app may live at http://localhost:8080/app-name
, or http://foobars.com/
, or some other URL. I do not have any guarantees about the absolute path - sometimes the call may need to go to /app-name/foo
, sometimes to /foo
, sometimes to /foobars/foo
. In other words, there is a moving part: /app-name
, (empty)
, /foobars
in samples above. Also known as context path.
我知道我可以使用<碱基GT;
的HTML。我可以换 $ HTTP
或配置Restangular为所有呼叫prePEND上下文路径。
I know I can use <base>
for HTML. I can wrap $http
or configure Restangular to prepend context path for all calls.
但仍然有,我还不知道如何处理案件。举例来说,当指令使用的模板,它是通过裸 $ HTTP
以及载入。
But there still are cases that I don't yet know how to address. For instance, whenever directive uses a template, it is loaded via bare $http
as well.
我如何获得 $ HTTP
总是使用一些特定的基本路径为所有请求?
How can I get $http
to always use some specific base path for all requests?
推荐答案
您将要配置的$ httpProvider使用该修改的调用$ HTTP将使用配置的拦截器。
You will want to configure the $httpProvider to use an Interceptor that modifies the config that the call to $http will use.
查看拦截器部分在这里: http://docs.angularjs.org/api/ng 。$ HTTP
See the Interceptors section here: http://docs.angularjs.org/api/ng.$http
这将允许您控制您需要的所有$ HTTP调用任何内容相关的路径。
This will allow you to control your contextual path in any way you need for all $http calls.
这篇关于角JS:如何设置上下文路径通过HTTP $所有呼叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!