Angular - 使用 ngNotificationsBar 时发现循环依赖

jcu*_*bic 1 javascript exception angularjs

我尝试使用ng-notifications-bar模块,我有这样的代码:

angular.module('app', [
    uiRouter,
    Common.name,
    Components.name,
    angularMaterial,
    'ngTable',
    'gridster',
    'ngNotificationsBar'
  ])
  .factory('$exceptionHandler', ['notifications', function(notifications) {
    return function(exception, cause) {
      notifications.showError({message: exception});
    };
  }]);
Run Code Online (Sandbox Code Playgroud)

但有错误:

[$injector:cdep] 找到循环依赖:$rootScope <-通知 <- $exceptionHandler <- $rootScope <- $timeout <- $$rAF <- $mdGesture

我试图修改库来使用$injector,以获得$超时和$ rootScope,但没有帮助还试图用$injector获得notifications$exceptionHandler工厂,但得到了同样的错误。

Mat*_*Way 5

从角度来看,这个设计非常糟糕。由于依赖关系,您不能$rootScope以任何形式注入$exceptionHandler

您可以使用$injector来绕过这些类型的(超出您的手的)依赖问题,您只需要确保在返回函数内部使用注入的模块,以确保在调用.get()依赖模块时实际上已经加载。例如:

// won't not be available here
var rootScope = $injector.get('$rootScope');

return function(exception, cause) {
  // will be available here
  var rootScope = $injector.get('$rootScope');
};
Run Code Online (Sandbox Code Playgroud)

这是因为$injector用于在运行时获取依赖项。