我已经修饰了AngularJS $日志服务,但我只能看到它加载配置并且没有任何$ log方法被装饰。这曾经工作,我在应用程序中有很多$log.info(...);
我需要为部署关闭,但即使设置了生产,它们仍然会被记录到控制台。为简洁起见,我简化了为简洁信息而修改的方法。
任何人都可以看到发生了什么或者错了什么?这是一本非常好的教科书,当我注意到它不再有效时,我检查了一堆来源,他们看起来也是这样做的。
(function () {
'use strict';
/**
* Registers a decorator for the $log service.
* @param {Object} $provide
*/
function logConfig($provide) {
console.log('CAN SEE CONFIG BOOTSTRAP');
$provide.decorator('$log', extendLogHandler);
}
logConfig.$inject = [
'$provide'
];
/**
* Turns off logging to the console based on the environment set in the configuration
* settings as long as logging was performed through AngularJS' $log component.
* @param {Object} $delegate Service instance of $log to be decorated
* @param {Object} APP_CONFIG
* @returns {Object}
*/
function extendLogHandler($delegate,
APP_CONFIG) {
// Saving the original behaviour
var _info = $delegate.info;
// Supplant existing behaviour by stipulating the application must be
// in development mode in order to log
$delegate.info = function (msg, data) {
console.log('NOT INVOKED');
// Prevent all logging unless in development
if (APP_CONFIG.ENV !== 'production') {
// Replace the original behavior
if (angular.isDefined(data)) {
_info(msg, data);
}
else {
_info(msg);
}
}
};
// Provide the supplanted $log delegate
return $delegate;
}
extendLogHandler.$inject = [
'$delegate',
'APP_CONFIG'
];
angular
.module('app')
.config(logConfig);
})();
更新
似乎问题与此其他配置相关,该配置在ngTable事件发生时将应用程序滚动到顶部:
function table(UtilsFactoryProvider,
ngTableEventsChannelProvider) {
// Reference to new page generation event listener
var newPageGenerationListener = null;
var ngTableEventsChannel = ngTableEventsChannelProvider.$get();
var UtilsFactory = UtilsFactoryProvider.$get();
// Subscribe to new instances of NgTableParams
ngTableEventsChannel
.onAfterCreated(function () {
// Set the previous page default for new instance
var prevPage = null;
// Deregister the any existing page generation event listener
if (typeof newPageGenerationListener === 'function') {
// Release memory to prevent leaks
newPageGenerationListener();
}
// Subscribe to new page generation event listener
newPageGenerationListener = ngTableEventsChannel
.onPagesChanged(function (events) {
// Set the next page
var nextPage = events.page();
// Only scroll to the top if not a new instance
if (prevPage !== null) {
UtilsFactory
.scrollTop(true);
}
prevPage = nextPage;
});
});
}
table.$inject = [
'UtilsFactoryProvider',
'ngTableEventsChannelProvider'
];