0

私はこれを理解することはできません。injection現在、より多くのドキュメントに飛び込んでいますが、なぜこれが機能しないのかわかりません。以下の例では、undefinedmyConstants.defaultPathとしてログに記録します。

(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);
4

1 に答える 1

0

定数を登録することをお勧めします。これmodule.constantは、両方configの方法と他の方法で利用できます。

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

構成に注入 (注:myConstantsではありませんmyConstantsProvider):

config.$inject = ['$routeProvider', '$httpProvider', 'myConstants'];
于 2013-08-30T20:04:58.143 に答える