Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access $window in a Provider?

Tags:

angularjs

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?

like image 960
Luis Masuelli Avatar asked Oct 21 '14 15:10

Luis Masuelli


1 Answers

Inject $windowProvider and call $windowProvider.$get()

like image 107
New Dev Avatar answered Oct 02 '22 09:10

New Dev