Javascript 如何正确覆盖angularjs中的exceptionHandler?
对于一个应用程序,我使用的框架非常类似于 我在services.js中尝试过类似的方法:Javascript 如何正确覆盖angularjs中的exceptionHandler?,javascript,angularjs,Javascript,Angularjs,对于一个应用程序,我使用的框架非常类似于 我在services.js中尝试过类似的方法: 'use strict'; /* Services */ angular.module('mApp.services', []). factory('$exceptionHandler', function () { return function (exception, cause) { alert(exception.message); } }); 这没有任何作
'use strict';
/* Services */
angular.module('mApp.services', []).
factory('$exceptionHandler', function () {
return function (exception, cause) {
alert(exception.message);
}
});
这没有任何作用,它似乎没有覆盖exceptionHandler
如果我尝试:
var mod = angular.module('mApp', []);
mod.factory('$exceptionHandler', function() {
return function(exception, cause) {
alert(exception);
};
});
它覆盖了整个应用程序
如果我使用与默认angular应用程序类似的框架,如何正确覆盖exceptionHandler?如果不查看应用程序的其余部分,很难确定,但我猜
angular.module('myApp')。工厂(…
将起作用。如果忽略第二个参数(,[]
)angular将检索现有模块以进行进一步配置。如果您将其保留,angular将创建一个新模块。尝试此示例
您是对的,删除[]有效。但是$exceptionHandler是一项服务吗?在skel应用程序中,服务是这样加载的:是的,它是angular提供的服务:-您使用此服务创建自己的异常处理程序(您不是在创建新服务)
var myApp = angular.module('myApp', ['ng']).provider({
$exceptionHandler: function(){
var handler = function(exception, cause) {
alert(exception);
//I need rootScope here
};
this.$get = function() {
return handler;
};
}
});
myApp.controller('MyCtrl', function($scope, $exceptionHandler) {
console.log($exceptionHandler);
throw "Fatal error";
});