I've created a factory to provide my own implementation of Angular's $exceptionHandler. This factory emits an event on $rootScope to signal listeners that an error has occurred. My listener is defined in a directive which is watching for the $rootScope.$on event. Everything seems fine insofar as my factory instance intercepts the exception and emits the error event. However, the directive which is supposedly listening for the event, never receives said event. What is the disconnect that is causing this event to be missed by the listener in the directive??
Clarification: The eventing relies on emitting and consuming entirely on $rootscope so as to avoid broadcasting events downstream. This is by design and has previously worked, or at least appears to work in an httpLoader and an httpErrorHandler that I wrote based off of several implementations which used the same strategy. http://plnkr.co/edit/N0JwYJdxPLur4xYZTUP1?p=info
Plunker: http://plnkr.co/edit/LDbRa0Ew255fOd0EunAq?p=preview
angular.module('demo.services',[]);
angular.module('demo.directives',[]);
angular.module('demo',['demo.services','demo.directives']);
angular.module('demo')
.controller('demoCtrl', function($scope){
$scope.message="Generate Error";
$scope.generateError =function(){
var x;
x.foo.oops = ':(';
}
});
angular.module('demo.services')
.factory('$exceptionHandler', function () {
var $injector = angular.injector(['ng']);
return function errorCatcherHandler(exception, cause) {
rootScope = $injector.get('$rootScope');
rootScope.$emit("httpError",exception);
console.error(exception);
};
});
angular.module('demo.directives')
.directive('ngHttpError', ['$rootScope',
function ($rootScope) {
return {
scope: {
title: '@',
},
template: '<div ng-click="showError=false"' +
'ng-show="showError">'+
'ERROR! {{errorModel | json}}' +
'</div>',
link: function ($scope) {
var showError;
$scope.showError = false;
$scope.errorModel = {};
showError = $scope.showError;
var initScope = function(e,m){
console.log(m);
$scope.errorModel = m;
$scope.showError=true;
}
$rootScope.$on("httpError", initScope);
}
};
}
]);
angular.bootstrap(document,['demo']);