我已经加入了AngularJS,烤面包机库我的index.html:
I have added the AngularJS-Toaster library to my index.html:
<link href="lib/angularjs-toaster/toaster.min.css" rel="stylesheet" />
<script src="lib/angularjs-toaster/toaster.min.js"></script>
<toaster-container
toaster-options="{'position-class': 'toast-bottom-center'}">
</toaster-container>
在我的角度应用程序:
And in my angular app:
//app.js
(function () {
angular
.module('app', ['toaster','blocks.logger',/*etc*/]);
})();
//blocks/logger/logger.module.js
(function () {
angular.module('blocks.logger', ['toaster']);
})();
//blocks/logger/logger.js
(function () {
'use strict';
angular
.module('blocks.logger')
.factory('logger', logger);
logger.$inject = ['$log', 'toaster'];
function logger($log, toaster) {
var service = {
error: error,
info: info,
success: success,
warning: warning,
log: $log.log // straight to console;
};
return service;
function error(message, title, data) {
toaster.error({ title: title || 'Error', body: message, timeout: 6000 });
$log.error(message, data);
}
//etc
}());
但我做到这一点,当我从注入的循环依赖关系错误:未捕获的错误:[$喷油器:CDEP]
我在做什么错了?
修改我发现这个问题是相关的,我已经使用异常处理程序:
EDIT I have discovered that the problem is related to an exception handler that I have used:
//code lifted from https://github.com/johnpapa/ng-demos (modular)
(function () {
'use strict';
angular
.module('blocks.exception')
.provider('exceptionHandler', exceptionHandlerProvider)
.config(config);
function exceptionHandlerProvider() {
/* jshint validthis:true */
this.config = {
appErrorPrefix: undefined
};
this.configure = function (appErrorPrefix) {
this.config.appErrorPrefix = appErrorPrefix;
};
this.$get = function () {
return { config: this.config };
};
}
function config($provide) {
$provide.decorator('$exceptionHandler', extendExceptionHandler);
}
//function extendExceptionHandler($delegate, exceptionHandler, logger) {
function extendExceptionHandler($delegate, exceptionHandler) {
return function (exception, cause) {
var appErrorPrefix = exceptionHandler.config.appErrorPrefix || '';
var errorData = { exception: exception, cause: cause };
var logMessage = appErrorPrefix + exception.message;
$delegate(exception, cause);
//logger.error(logMessage, errorData);
};
}
})();
通过注释掉使用记录器,我可以用在其他地方登录。不过,我想在这里用我的记录了。那么,什么是怎么回事,我该怎么解决这个问题?
By commenting out the use of the logger, I can use logging elsewhere. But I'd like to use my logger here too. So what is going on and how do I fix it?
我找到了解决办法 - 使用 $注射器
中的异常处理程序:
I found a solution - use $injector
in the exception handler:
//blocks/exception/exception.module.js
(function () {
'use strict';
angular.module('blocks.exception', ['blocks.logger']);
})();
//blocks/exception/exceptionHandlerProvider.js
(function () {
'use strict';
angular
.module('blocks.exception')
.config(exceptionConfig);
exceptionConfig.$inject = ['$provide'];
function exceptionConfig($provide) {
$provide.decorator("$exceptionHandler", exceptionHandler);
}
exceptionHandler.$inject = ['$delegate', '$injector'];
function exceptionHandler($delegate, $injector) {
return function (exception, cause) {
var logger = $injector.get('logger');
logger.error(exception.message);
$delegate(exception, cause);
};
}
})();
参考文献:
$log装饰与服务的依赖将导致循环依赖错误
角风格指南 - 例外