javascript - Angular - circular dependency injector whie trying inject $modal to exception handler decorator -
due john papa - 10 angularjs patterns presentation advices try implement additional functionality exception handling:
exceptionhandlerdecorator.$inject = ['$provide']; function exceptionhandlerdecorator($provide){ $provide.decorator('$exceptionhandler', handleexception); } handleexception.$inject = ['$delegate', 'exceptionhandlerservice']; function handleexception($delegate, exceptionhandlerservice){ function handle(exception, cause){ $delegate(exception, cause); exceptionhandlerservice.handle(exception.message); } return handle; } exceptionhandlerservice.$inject = ['$modal']; function exceptionhandlerservice($modal){ //do things }
but when try ti inject $modal
exceptionhandlerservice
angular ui bootstrap got error: $injector:cdep circular dependency terrified me. tried use accepted solution similar question, injecting $http angular factory($exceptionhandler) results in circular dependency:
function exceptionhandlerservice($window, $injector){ var $modal = $injector.get('$modal') }
but gave me same result - error: $injector:cdep circular dependency
. has had similar problem , knows solution? thank in advance attention.
the issue if do,
function exceptionhandlerservice($window, $injector){ var $modal = $injector.get('$modal') }
it try instantiate $modal
service exceptionhandlerservice instantiated via decorator, cause cdep error. want instead $modal
instance lazily when needed , must not try instantiate (or it) during service instantiation process. i.e:
function exceptionhandlerservice($window, $injector){ function _getmodal(){ //you can afford everytme service singleton , //the ioc container have instance once instantiated act asa getter. return $injector.get('$modal'); } function logstuff(){ _getmodal().modal(.... } }
Comments
Post a Comment