我已经装饰了 AngularJS $log 服务,但我只能看到它加载配置并且没有装饰任何 $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'
];