角度配置不起作用

时间:2015-03-20 18:47:08

标签: angularjs dependency-injection factory

我有以下代码。当我在其他代码中注入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
       }
    }
}]);

1 个答案:

答案 0 :(得分:1)

你倒了你的注射。

['$http', 'apiServerConstants', 'localStorageService', function($http, localStorageService, apiServerConstants)应为['$http', 'localStorageService', 'apiServerConstants', function($http, localStorageService, apiServerConstants)

修改

为什么apiServerConstantslogin的参数?试试这个:

login: function(data) {
    console.log(apiServerConstants.url);
}