angular.module('app',[])
.factory('httpRequestInterceptor','APP_VERSION', function (APP_VERSION) {
var DNAME = sessionStorage.getItem('DNAME');
var DID = localStorage.getItem('uid');
return {
request: function (config) {
config.headers['Browser-Agent'] = 'Abc/'+DNAME+'/'+DID+'/'+APP_VERSION+'';
return config;
}
};
})
.config(function($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});
我得到以下错误。
未捕获错误:[$injector:modulerr] 由于以下原因无法实例化模块应用: 错误:[$injector:strictdi] 函数 ($httpProvider( 未使用显式注释,无法在严格模式下调用
工厂缺少方括号:
angular.module('app',[])
̶.̶f̶a̶c̶t̶o̶r̶y̶(̶'̶h̶t̶t̶p̶R̶e̶q̶u̶e̶s̶t̶I̶n̶t̶e̶r̶c̶e̶p̶t̶o̶r̶'̶,̶ ̶'̶A̶P̶P̶_̶V̶E̶R̶S̶I̶O̶N̶'̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶(̶A̶P̶P̶_̶V̶E̶R̶S̶I̶O̶N̶)̶ ̶{̶
.factory('httpRequestInterceptor',['APP_VERSION', function (APP_VERSION) {
var DNAME = sessionStorage.getItem('DNAME');
var DID = localStorage.getItem('uid');
return {
request: function (config) {
config.headers['Browser-Agent'] = 'Abc/'+DNAME+'/'+DID+'/'+APP_VERSION+'';
return config;
}
};
̶}̶)̶
}])
并且.config
缺少显式注释:
̶.̶c̶o̶n̶f̶i̶g̶(̶f̶u̶n̶c̶t̶i̶o̶n̶(̶$̶h̶t̶t̶p̶P̶r̶o̶v̶i̶d̶e̶r̶)̶ ̶{̶
.config(["$httpProvider", function($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
̶}̶)̶
}]);
有关详细信息,请参阅
- AngularJS 开发人员指南 - 依赖注入 - 内联数组注释
- AngularJS 错误参考 -
$injector:strictdi
需要显式注释
httpRequestInterceptor
后删除APP_VERSION
参数,它应该可以工作。
angular.module('app',[])
.factory('httpRequestInterceptor', function (APP_VERSION) {