首页 > Web开发 > 详细

[AngularJS] Catching errors with $exceptionHandler

时间:2015-03-07 18:33:59      阅读:302      评论:0      收藏:0      [点我收藏+]

The AngularJS $exceptionHandler service allows you to catch and handle unanticipated JavaScript errors in a meaningful way.

So when application is under building process, can create a $exceptionHandler service to log out the uncatch exception.

 

angular.module(‘app‘, [])
    .factory(‘$exceptionHandler‘, function ($injector) {
        return function (exception, cause) {
            var $rootScope = $injector.get(‘$rootScope‘);
            $rootScope.errors = $rootScope.errors || [];
            $rootScope.errors.push(exception.message);
            console.log($rootScope.errors);
        }
    })
    .run(function ($http) {

        function onSuccess (result) {
            console.log(‘hooray data!‘);
            console.log(result.data.length, ‘repos found‘);
            result.count(); // This is no count() method on the result object.
        }

        function onFailure (info) {
            console.log(‘boo error :(‘);
            console.log(info);
        }

        $http.get(‘https://api.github.com/users/bclinkinbeard/repos‘)
            .then(onSuccess, onFailure); // We can catch $http return failure but there could be some uncatch failure in some place
    });

 

[AngularJS] Catching errors with $exceptionHandler

原文:http://www.cnblogs.com/Answer1215/p/4320681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!