【问题标题】:How to properly overwrite the exceptionHandler in angularjs?如何正确覆盖angularjs中的exceptionHandler?
【发布时间】:2013-05-10 18:57:14
【问题描述】:

对于一个应用,我使用的骨架与https://github.com/angular/angular-seed 非常相似。

我在 services.js 中尝试过这样的事情:

'use strict';

/* Services */

angular.module('mApp.services', []).
  factory('$exceptionHandler', function () {
    return function (exception, cause) {
        alert(exception.message);
    }
});

这并没有做任何事情,它似乎没有覆盖异常处理程序。

如果我尝试:

var mod = angular.module('mApp', []);

mod.factory('$exceptionHandler', function() {
  return function(exception, cause) {
    alert(exception);
  };
});

它会覆盖整个应用程序。

如果我使用类似于默认 Angular 应用的骨架,如何正确覆盖 exceptionHandler?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    如果不查看应用程序的其余部分,很难确定,但我猜angular.module('myApp').factory( ... 会起作用。如果您省略第二个参数 (,[]),则 angular 将检索现有模块以进行进一步配置。如果你保持它的角度将创建一个新的模块。

    【讨论】:

    【解决方案2】:

    试试这个例子

    http://jsfiddle.net/STEVER/PYpdM/

    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";
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2012-05-30
      相关资源
      最近更新 更多