将工厂注入配置块

时间:2013-08-30 19:57:10

标签: javascript angularjs

我无法想出这个。目前潜入更多injection个文档,但我不确定为什么这不起作用。在下面的示例中,myConstants.defaultPath记录为 undefined

(function() {
  angular.module('my-constants', []).factory('myConstants', [
    function() {
      var service;
      return service = {
        defaultPath: '/aroute'
      };
    }
  ]);

}).call(this);


(function() {
  var app, config;

  app = angular.module('my-app', ["my-constants"]);

  config = function($routeProvider, $httpProvider, myConstants) {

    console.log(myConstants.defaultPath); // undefined

    return $routeProvider.otherwise({
      redirectTo: myConstants.defaultPath
    });
  };

  config.$inject = ['$routeProvider', '$httpProvider', 'myConstantsProvider'];

  app.config(config);

}).call(this);

1 个答案:

答案 0 :(得分:0)

我建议您使用module.constant注册常量,config和任何其他方法都可以使用。{/ p>

angular.module('my-constants', []).constant('myConstants', {
  defaultPath: '/aroute'
})

注入配置(注意:myConstants而非myConstantsProvider):

config.$inject = ['$routeProvider', '$httpProvider', 'myConstants'];
相关问题