2017-05-11 79 views
0

我AngularJS工作,typescript.I也呼籲REST服務從backend.My問題檢索數據,處理全局錯誤是什麼,是在全球範圍內處理呼叫誤差爲所有controller.Thank的提前,最好的辦法在AngularJS

回答

0

在全球範圍內處理錯誤的最好方式就是它覆蓋

'$ exceptionHandler的'

這裏是文檔鏈接 「https://docs.angularjs.org/api/ng/service/ $ exceptionHandler的」

(function() { 
    'use strict'; 

    var app = angular.module('app'); 

    // Configure by setting an optional string value for appErrorPrefix. 
    // Accessible via config.appErrorPrefix (via config value). 

    app.config(['$provide', function ($provide: ng.auto.IProvideService) { 
     $provide.decorator('$exceptionHandler', 
      ['$delegate', 'config', 'logger', extendExceptionHandler]); 
    }]); 

    // Extend the $exceptionHandler service to also display a toast. 
    function extendExceptionHandler($delegate: ng.IExceptionHandlerService, config: config, logger: logger) { 
     var appErrorPrefix = config.appErrorPrefix; 
     var logError = logger.getLogFn('app', 'error'); 
     return function (exception: Error, cause: string) { 
      $delegate(exception, cause); 
      if (appErrorPrefix && exception.message.indexOf(appErrorPrefix) === 0) { return; } 

      var errorData = { exception: exception, cause: cause }; 
      var msg = appErrorPrefix + exception.message; 
      logError(msg, errorData, true); 
     }; 
    } 
})(); 

https://github.com/johnnyreilly/HotTowel-Angular-TypeScript/blob/master/app/config.exceptionHandler.ts