I create a provider to retrieve configuration of application via a Json file. Then i use it in my application.config to instiante route from this configuration object.
MyApp = angular.module('MyApp', ['MyAppControllers','LocalStorageModule','ui.router']);
This is the code of my configuration provider :
MyApp.provider('$configuration', function() {
return {
$get : function($http,localStorageService) {
return {
getConfiguration:function() {
var config = localStorageService.get("configuration");
console.log(config);
if(config ==null || config == undefined) {
xhr = GetHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 304) {
localStorageService.set("configuration",xhr.responseText);
config = xhr.responseText;
} else {
console.log('## Error loading configuration');
}
}
}
xhr.open('GET','/assets/js/My/config.json',false);
xhr.send(null);
return JSON.parse(config);
} else {
return config;
}
}
}
}
}
});
Here the code of my routes configuration :
MyApp.config(['$stateProvider', '$urlRouterProvider','$configurationProvider',
function ($stateProvider, $urlRouterProvider,$configurationProvider) {
console.log($configurationProvider);
configuration=$configurationProvider.getConfiguration();
angular.forEach(configuration.url, function(value, key) {
$stateProvider.state(stateName,
{
url: stateUrl,
views: stateView
}
);
});
}
]);
But chrome give me this error :
[$injector:modulerr] Failed to instantiate module MyApp due to: TypeError: Cannot read property 'get' of undefined