Javascript ui路由器包装中的Angular.js循环依赖项错误
我遵循John Papa的angular style guide()并在本指南中提供的angular ui路由器周围使用自定义包装器。但是,包装器不适用于我,在注入$state时,我会遇到一个循环依赖项错误:Javascript ui路由器包装中的Angular.js循环依赖项错误,javascript,angularjs,angular-ui-router,angularjs-injector,Javascript,Angularjs,Angular Ui Router,Angularjs Injector,我遵循John Papa的angular style guide()并在本指南中提供的angular ui路由器周围使用自定义包装器。但是,包装器不适用于我,在注入$state时,我会遇到一个循环依赖项错误: Uncaught Error: [$injector:cdep] Circular dependency found: $rootScope <- $timeout <- $$rAF <- $$animateQueue <- $animate <- toast
Uncaught Error: [$injector:cdep] Circular dependency found: $rootScope <- $timeout <- $$rAF <- $$animateQueue <- $animate <- toastr <- logger <- $exceptionHandler <- $rootScope <- $state <- routerHelper
我认为这是toastr的问题,而不是UI路由器代码的问题 约翰·帕帕(John Papa)的例子基于普通的“toastr”包,而不是“angular toastr”包 托马斯特: 有角toastr: 使用“toastr”包,他使用常量注册toastr的全局实例:
.module('app.core')
.constant('toastr', toastr);
这使其可注入记录器服务:
logger.$inject = ['$log', 'toastr'];
/* @ngInject */
function logger($log, toastr) {
但是,如果使用angular toastr包,则toastr对象会对某些角度对象引入一组依赖项:
$rootScope <- $timeout <- $$rAF <- $$animateQueue <- $animate <- toastr
toastr <- logger <- $exceptionHandler <- $rootScope
尝试在HotTower模板上使用angular toastr时遇到相同问题。使用$inject延迟依赖关系对我来说很有效。正如@Casey所说,我不确定这是否正确。
toastr <- logger <- $exceptionHandler <- $rootScope
logger.$inject = ['$log', '$injector']; // 'toastr'
/* @ngInject */
function logger($log, $injector) { // toastr
var service = {
showToasts: true,
info : info,
success : success,
warning : warning,
error : error,
// straight to console; bypass toastr
log : $log.log
};
return service;
/////////////////////
function info(message, data, title) {
var toastr = $injector.get('toastr');
toastr.info(message, title);
$log.info('Info: ' + message, data);
}
function success(message, data, title) {
var toastr = $injector.get('toastr');
toastr.success(message, title);
$log.info('Success: ' + message, data);
}
function warning(message, data, title) {
var toastr = $injector.get('toastr');
toastr.warning(message, title);
$log.warn('Warning: ' + message, data);
}
function error(message, data, title) {
var toastr = $injector.get('toastr');
toastr.error(message, title);
$log.error('Error: ' + message, data);
}
}