我想将我的<base href="...
href 属性的值添加到所有 get 和 post$http
调用中。我尝试使用如下请求拦截器:
angular.module('curriculumModule', ['ng', 'ngRoute', 'curriculumControllerModule', 'directiveModule'])
.config(function($routeProvider, $httpProvider) {
$httpProvider.defaults.headers.common['Content-Type'] = 'application/json';
$httpProvider.defaults.headers.common['X-Ajax'] = 'true';
$httpProvider.interceptors.push(function ($q) {
return {
'request': function (config) {
config.url = $('base').attr('href') + '/' + config.url;
return config || $q.when(config);
}
};
});
$routeProvider
.when('/view/:id', {controller: 'ViewCurriculumCtrl', templateUrl: 'view.html'})
.when('/new', {controller: 'CreateCurriculumCtrl', templateUrl: 'new.html'})
.when('/edit/:id', {controller: 'EditCurriculumCtrl', templateUrl: 'edit.html'})
.when('/:curriculumId/workExperience/new', {controller: 'NewWorkExperienceCtrl', templateUrl: 'workExperiencesNew.html'})
.when('/:curriculumId/workExperience/edit/:id', {controller: 'EditWorkExperienceCtrl', templateUrl: 'workExperiencesEdit.html'})
.when('/:curriculumId/training/new', {controller: 'NewTrainingCtrl', templateUrl: 'trainingsNew.html'})
.when('/:curriculumId/training/edit/:id', {controller: 'EditTrainingCtrl', templateUrl: 'trainingsEdit.html'})
.otherwise({redirectTo:'/view/:id'});
});
但是,不知何故,AngularJs 还将 base href 属性的值添加到我不想要的 templateUrls 中。
请问我该如何防止这种情况?