I'd like to access $window
from a provider (mymodule.provider(...)
) BUT I need such dependency at config time (so it's no use for me to put such dependency in the $get
).
Currently I don't access $window
, and the reason I need such dependency in config time is because I need to access $window.STATIC_URL
, which is a constant in the window, and I use it to configure the states' templates ($stateProvider
).
Current code is:
mymodule.provider('StaticUrl', function() { this.url = function() { return window.STATIC_URL || 'static'; }; this.$get = this.url; });
This is because I need such value in both config and run time (I use it for more than just setting up the states, so when I need such url in a controller, I import it as a dependency).
//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'; }]);
Given an existing dataset, I have a picture for each entry, located under STATIC_URL, which must be provided. So this means I use such constant in both stages.
How would I do it in a more Angular-way (i.e. without relying in globals)? Is there a way I can access $window
from a provider without raising a moduleerr
?
Inject $windowProvider
and call $windowProvider.$get()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With