我有以下代码。当我在其他代码中注入AuthService时,工厂中的console.log(apiServerConstants.url)
行返回undefined
。为什么呢?
angular.module("webclient.constants", [])
.constant("apiServerConstants", {
"url": "http://localhost:8080"
});
angular.module('webclient', [
// some stuff
'webclient.constants'
])
.config(
// some code
});
angular.module('webclient')
.factory('AuthService', ['$http', 'apiServerConstants', 'localStorageService', function($http, localStorageService, apiServerConstants) {
return {
authenticate: function(data) {
// some code
},
login: function(data, apiServerConstants) {
console.log(data);
console.log(apiServerConstants.url);
// some more code
}
}
}]);
答案 0 :(得分:1)
你倒了你的注射。
['$http', 'apiServerConstants', 'localStorageService', function($http, localStorageService, apiServerConstants)
应为['$http', 'localStorageService', 'apiServerConstants', function($http, localStorageService, apiServerConstants)
修改强>
为什么apiServerConstants
是login
的参数?试试这个:
login: function(data) {
console.log(apiServerConstants.url);
}