I have a directive that I am injecting a service into that makes $http calls to a back end service.
How do you handle 404/401/HTTP errors in general? I am looking for a best practice pattern.
Does $http gobble up and reject the promise when it hits a HTTP error?
This is what I have so far, and it seems to work OK, but I am not sure I am doing the recommended way:
Service
app.service('helpService', ['$http', function ($http) {
return {
getHelpUrl: function (pageUrl) {
return $http.post('Home/GetHelpUrl', { pageUrl: pageUrl });
}
}
}]);
Directive
app.directive('helpLink', ['$location', 'helpService', function ($location, helpService) {
return {
restrict: 'A',
replace: true,
scope: {},
template: function (elem, attrs) {
return '<a ng-href="{{helpUrl}}" ng-show="showLink" target="_blank">Help?</a>';
},
link: function (scope, elem, attrs) {
scope.showLink = false;
helpService.getHelpUrl($location.path()).success(function (data) {
scope.helpUrl = data.helpUrl;
scope.showLink = true;
});
}
}
}]);