A rough example of how I've been handling errors
var pageModule = angular.module('pageModule',[])
.service('myService',function($http) {
return {
someHttpCall : function() {
return $http.get('myHttpCall');
}
}
})
.controller('parentCtrl',function($scope,myService) {
myService.someHttpCall()
.success(function(response) {
//
})
.error(function(response) {
$scope.$emit('errorEvent', {
type : 'networkError',
message : 'There was a network error with this ajax call'
});
});
$scope.someFunction = function() {
if(error) {
$scope.$emit('errorEvent', {
type : 'myErrorType',
message : 'My message about error here'
});
}
}
})
.controller('childCtrl',function($scope) {
$scope.someFunctionInChildScope = function() {
if(error) {
$scope.$emit('errorEvent', {
type : 'myOtherErrorType',
message : 'My other message about error here'
});
}
}
})
.directive('myErrorMessage',function($rootScope) {
return {
link : function($scope,$element,$attrs) {
$rootScope.$on('errorEvent',function(event,errorObj) {
//compile and append an error here.. based on errorObj
});
}
}
});