簡體   English   中英

將工廠注入配置塊

[英]Injecting factories into config blocks

我無法弄清楚這一點。 目前正在研究更多的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);

我建議您向module.constant注冊常量, module.constantconfig和其他任何方法中都可用。

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

注入配置(注意: myConstants不是myConstantsProvider ):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM