我想创建一些“存储库”,并为所有请求添加基本URL,但仅限于该存储库。
angular.module('user', [])
.service('User', function ($http) {
// SOMETHING LIKE THIS
$http.setBaseUrl('/api/v1/users');
this.index = function () {
// CALL TO api/v1/users/
return $http('/');
}
});
我知道有$ httpProvider,我可以添加拦截器,但它会添加到所有请求,这不是我想要的。
我该怎么办?
答案 0 :(得分:1)
您可以创建一个包含各种基本URL字符串的constant.js服务文件,并且只要您需要进行$ http调用,就可以调用该特定的基本URL。
像...一样的东西。
$http.post(constants.usersURL + "/", data, function(res) {
... //returned val
});
constants.js文件如下所示:
angular.module('yourApp')
.factory('constants', function () {
var shared = {
baseURL: "/api/v1/",
usersURL: "/api/v1/users"
}
return shared;
});