alexcrack / angular-ui-notification

Angular.js service providing simple notifications using Bootstrap 3 styles with css transitions for animating
MIT License
536 stars 169 forks source link

Circular dependency in interceptors #32

Closed brenjt closed 9 years ago

brenjt commented 9 years ago

I am having this exact same issue: https://github.com/alexcrack/angular-ui-notification/issues/29

I tried the code and I did not have any success.

angular.module('ui', [])
    .config(function($httpProvider) {
        $httpProvider.interceptors.push('baseHttpInterceptor');
    })
   .factory('baseHttpInterceptor', function ($injector) {

       var Notification = $injector.get('Notification');

       return {
           'responseError': function() {
               Notification.error('Oops');
           }
       };
    });

Circular dependency error:

Circular dependency found: 
$compile <- Notification <- baseHttpInterceptor <- $http <- $templateRequest <- $compile

Any ideas?

MarkPerryBV commented 9 years ago

I have just had the same problem. The issue arises that inside of "Notification" there is a dependency on $http and $templateCache.

What I have done and which seems to work is to delay the construction of Notification until it's needed in the interceptor.

.factory('baseHttpInterceptor', function ($injector) {
var notification = null;
        var getNotification = function() {
            if (!notification) {
                notification = $injector.get('Notification');
            }
            return notification;
        };
       return {
           'responseError': function() {
               getNotification().error('Oops');
           }
       };
    });
brenjt commented 9 years ago

Awesome, that worked great. Thank you! I would still like to see an actual fix though.

keon commented 9 years ago

@MarkPerryBV Thanks! I was having the same problem

renatomefi commented 9 years ago

thank you @MarkPerryBV :+1:

alexcrack commented 9 years ago

I think it wasn't the ui-notification's error. It's because angular's injector cannot correctly inject some services to interceptors. I've seen this trouble using another modules. So, I'm closing issue.