Reputation: 11177
Given the following, $injector.get
will fail as $rootScope
is not available.
app.factory('$exceptionHandler', ['$injector', $injector => {
const $rootScope = $injector.get('$rootScope')
return (exception, cause) => {
// code
}
})
In researching, I keep coming across this, which works as it's resolved at runtime (when the inner function is called).
app.factory('$exceptionHandler', ['$injector', $injector => {
return (exception, cause) => {
const $rootScope = $injector.get('$rootScope')
// code
}
})
What I'd like is to somehow know when I can resolve the dependency. Something like:
app.factory('$exceptionHandler', ['$injector', $injector => {
$injector.illLetYouKnowWhenWeCanDoStuff().then(() => {
const $rootScope = $injector.get('$rootScope')
})
return (exception, cause) => {
// code
}
})
Is this possible?
related => $location from $exceptionHandler - dependency conflict
Upvotes: 0
Views: 69
Reputation: 7066
All injectables registered through normal angular functions (factory, service, controller, etc) are available right away at bootstrap. $rootScope
isn't registered normally. It's provided after the app bootstraps. Because $exceptionHandler
is instantiated right away, $rootScope
isn't available yet.
The best way to get around a circular dependency error is to break the circle. Let your $exceptionHandler
depend on a custom service you build. Configure that new service in a .run()
block into which $rootScope
is injected.
app.factory('$exceptionHandler', ['myExceptionHandlingConfig', function(myExceptionHandlingConfig) {
return function(exception, cause) {
//do exceptional stuff
}
}]);
app.factory('myExceptionHandlingConfig', function() {
return {};
});
app.run(['$rootScope', 'myExceptionHandlingConfig', function($rootScope, myExceptionHandlingConfig){
myExceptionHandlingConfig.someProperty = $rootScope.something;
}]);
Note: $rootScope
will be pretty empty at bootstrap time, so you might want to set a $watch
to update the service when whatever data you're looking for is available.
Upvotes: 1