Encountering an issue with AngularJS dependency injection and timing. Below is the code snippet along with the error message:
var module = angular.module('Demo', []);
module.factory('demo', function () {
return {
data: {},
};
});
module.provider('foo', ['demo', function(demo) {
console.log(demo);
this.$get = function() {
};
}]);
Error Message:
Uncaught Error: [$injector:modulerr] Failed to instantiate module Demo due to:
Error: [$injector:unpr] Unknown provider: demo
Interestingly, adding a setTimeout function at the end resolves the issue. However, this is not the ideal solution as it's considered a hack.
var module = angular.module('Demo', []);
module.factory('demo', function () {
return {
data: {},
};
});
setTimeout(function(){
module.provider('foo', ['demo', function(demo) {
console.log(demo);
this.$get = function() {
};
}]);
});
For reference, here is the problem demonstrated on JSFiddle: http://jsfiddle.net/zcf7rb4s/1/