![](/img/trans.png)
[英]AngularJS Injecting a module into parent module and using child module run and config blocks
[英]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.constant
在config
和其他任何方法中都可用。
angular.module('my-constants', []).constant('myConstants', {
defaultPath: '/aroute'
})
注入配置(注意: myConstants
不是myConstantsProvider
):
config.$inject = ['$routeProvider', '$httpProvider', 'myConstants'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.