定义AngularJS工厂和控制器可以访问的全局函数和属性
Defining global functions and properties accessible by AngularJS factory and controllers
我正在开发一个AngularJS应用程序。当抛出错误时,我想对错误进行一些自定义处理。为此,我做了以下操作:
var myApp = angular.module('myApp', []);
myApp.factory('$exceptionHandler', function($rootScope) {
return function(exception, cause) {
try {
console.log('Unhandled error happening in version ' + $rootScope.APP_VERSION);
console.log('Error happened at ' + $rootScope.getFormattedDateTime());
} catch (uex1) {
console.log('Unable to log unhandled exception.');
}
};
});
myApp.run(function ($rootScope) {
$rootScope.APP_VERSION = '1.0';
$rootScope.getFormattedDateTime = function() {
return '--';
};
});
当我运行这段代码时,我得到这个错误。我想我不能将$rootScope
添加到工厂声明中。如果是这种情况,我如何定义全局可访问的函数和变量,以便我可以在我的控制器以及从这个工厂访问它们?
非常感谢您提供的任何帮助
你不能把$routeScope注入到工厂中,这不是一个好主意,但是
你能做的最好的事情就是定义一个新的工厂,并像这样定义你的属性:
app.factory('getAppInfoFactory',function(){
return{
getAppVersion:function(){
return APP_VERSION = '1.0';
},
getFormattedDateTime : function() {
return '--';
}
});
然后你可以随时随地使用这个工厂,像这样:
myApp.factory('$exceptionHandler', function(getAppInfoFactory) {
return function(exception, cause) {
try {
console.log('Unhandled error happening in version ' + getAppInfoFactory.getAppVersion());
console.log('Error happened at ' + getAppInfoFactory.getAppInfoFactory());
} catch (uex1) {
console.log('Unable to log unhandled exception.');
}
};
});
这绝对是最干净的方法。您可以很容易地用做同样事情的服务替换掉$filter,但是,我发现这种方法更简洁,因为它可以传递任意日期。
下面是演示下面代码的plunkr(Plunkr还包括一些奇特的记录错误的方法):http://plnkr.co/edit/iPMFTJ
angular
.module('app', [])
.constant({ APP_VERSION: '1.0' })
.config(function ($provide) {
function exceptionHandler($delegate, $filter, $log, APP_VERSION) {
return function handleException(exception, cause) {
var formatDateTime = $filter('formatDateTime');
try {
$log.error('Unhandled error happening in version ' + APP_VERSION);
$log.warn('Error happened at ' + formatDateTime(new Date()));
} catch (uex1) {
$log.info('Unable to log unhandled exception.');
}
// Include the line below if you want angular's default exception
// handler to run as well
// $delegate(exception, cause);
};
}
$provide.decorator("$exceptionHandler", exceptionHandler);
});
angular
.module('app')
.filter('formatDateTime', function formatDateTimeFilter($filter) {
return function formatDateTime(date) {
return $filter('date')(date, 'shortDate');
};
})
.controller('ErrorCtrl', function () {
throw new Error('testError');
});
相关文章:
- javascript 中的全局函数
- 从全局函数调用Ember控制器上的方法
- Firefox扩展开发:如何在内容脚本中创建一个全局函数,以便其他加载的脚本文件可以访问它
- Javascript-匿名函数中使用参数引用全局函数的闭包-我如何使用preventDefault
- 将我的nextPage()函数编写为全局函数,以便onclick事件可以访问它
- Ember.js-定义一个全局函数,该函数可以向操作返回值
- 如果有's是一个名为“”的全局函数;描述“;或“;它“;当使用诸如jasmine或mocha之类的测试框架时
- angularJS中的全局函数
- 如何在范围内创建全局函数
- JS恢复默认/全局函数
- 在方法中调用全局函数
- Javascript:全局变量转到“窗口”或“全局”,但全局函数不会.全球 FN 去哪儿了
- 高级闭包兼容性和静态类型 - 使用未定义类型和全局函数,如localStorage.getItem();
- 如何显式引用全局函数
- Javascript中是否有“全局函数”或“主函数”
- 使用离子框架改变全局函数的状态
- 如何在方法中添加事件侦听器时调用 removeEventLisener 并在 JavaScript 中的全局函数中调用
- 如何在 JavaScript 中覆盖全局函数,但保留对原始函数的引用
- 如何使用 JQuery 和 Ajax 写入全局函数
- JS:使全局函数成为对象的值