我想$window从提供者(mymodule.provider(...))访问,但我在配置时需要这样的依赖(所以我没有把这种依赖放在其中$get).
目前我不访问$window,我在配置时需要这种依赖的原因是因为我需要访问$window.STATIC_URL,这是窗口中的常量,我用它来配置状态的templates($stateProvider).
目前的代码是:
mymodule.provider('StaticUrl', function() {
this.url = function() { return window.STATIC_URL || 'static'; };
this.$get = this.url;
});
Run Code Online (Sandbox Code Playgroud)
这是因为我在配置和运行时都需要这样的值(我使用它不仅仅是设置状态,所以当我在控制器中需要这样的url时,我将它作为依赖项导入).
//config
mymodule.config(['StaticUrlProvider', '$stateProvider', function(sup, $sp){
//...
$sp.state('main', {
templateUrl: sup.url() + 'path/to/template.html'
});
//...
}]);
//run-time (in this case: a controller)
mymodule.controller('MyController', ['$scope', 'StaticUrl', function($s, su) {
var buildingType = /*a complex expression here*/;
$s.buildingPicture = $su + 'path/to/building/pics/' + buildingType + '.png';
}]);
Run Code Online (Sandbox Code Playgroud)
给定现有数据集,我为每个条目提供了一张图片,位于STATIC_URL下,必须提供.所以这意味着我在两个阶段都使用这样的常数.
我将如何以更加角度的方式(即不依赖于全局变量)进行此操作?有没有办法我可以$window从提供商访问而不提出moduleerr?
| 归档时间: |
|
| 查看次数: |
10357 次 |
| 最近记录: |