Angular 配置不工作
Angular Config Doesn't Work
我有以下代码。当我在其他代码中注入 AuthService 时,工厂 returns undefined
中的 console.log(apiServerConstants.url)
行。为什么?
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
}
}
}]);
你倒转了你的注射。
['$http', 'apiServerConstants', 'localStorageService', function($http, localStorageService, apiServerConstants)
应该是 ['$http', 'localStorageService', 'apiServerConstants', function($http, localStorageService, apiServerConstants)
编辑:
为什么apiServerConstants
是login
的参数?试试这个:
login: function(data) {
console.log(apiServerConstants.url);
}
我有以下代码。当我在其他代码中注入 AuthService 时,工厂 returns undefined
中的 console.log(apiServerConstants.url)
行。为什么?
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
}
}
}]);
你倒转了你的注射。
['$http', 'apiServerConstants', 'localStorageService', function($http, localStorageService, apiServerConstants)
应该是 ['$http', 'localStorageService', 'apiServerConstants', function($http, localStorageService, apiServerConstants)
编辑:
为什么apiServerConstants
是login
的参数?试试这个:
login: function(data) {
console.log(apiServerConstants.url);
}