This particular issue is quite perplexing to me, so I will do my best to break it down step by step for clarity.
1
Initially, I start the app by defining angular.module('app', [])
. Within this setup, I also add a value:
angular.module('app', []).value('Storage', {})
The value is set as {}
because there is no data to store at this point.
2
I have several "init" controllers to ensure smooth initialization. In the catalog controller, I intend to update the Storage
value.
Since I am in strict mode, my setup looks like this:
angular.module('app').controller('Init', InitController);
InitController.$inject = ['Storage', '$q', '$http'];
function InitController(Storage, $q, $http){
var getDeferred = $q.defer();
var got = getDeferred.promise;
$http({
// code to perform tasks
}).then(function(data){
getDeferred.resolve(data);
});
got.then(function(data){
// attempting to update and set the new value here
Storage = data;
});
}
3
In my Main Controller, following standard practices with injections:
angular.module('app').controller('Main', MainController);
MainController.$inject = ['Storage'];
function mainController(Storage){
console.log(Storage) // still displays an empty object
}
Feeling puzzled by this outcome, I revisit step two and consider utilizing the $provide
service to access the value differently. Will that be successful?
// Revised Step 2
angular.module('app').controller('Init', InitController);
InitController.$inject = ['$provide', '$q', '$http'];
function InitController($provide, $q, $http){
var getDeferred = $q.defer();
var got = getDeferred.promise;
$http({
// code to perform tasks
}).then(function(data){
getDeferred.resolve(data);
});
got.then(function(data){
// experimenting with a different approach
$provide.value('Storage', data);
});
}
This attempt leads to a complete crash and the error message:
Error: [$injector:unpr] http://errors.angularjs.org/1.5.7/$injector/unpr?p0=[object Object]$provideProvider%%3C-%%24provide%%3C-%CatalogInit
Despite having all dependencies correctly defined, this error remains confusing. Even after searching online, I find myself stuck with repetitive information.