Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngInclude): emit $includeContentError when HTTP request fails #5825

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/ng/directive/ngInclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@
* @description
* Emitted every time the ngInclude content is reloaded.
*/


/**
* @ngdoc event
* @name ng.directive:ngInclude#$includeContentError
* @eventOf ng.directive:ngInclude
* @eventType emit on the scope ngInclude was declared in
* @description
* Emitted when a template HTTP request yields an erronous response (status < 200 || status > 299)
*/
var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$animate', '$sce',
function($http, $templateCache, $anchorScroll, $animate, $sce) {
return {
Expand Down Expand Up @@ -227,7 +237,10 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$animate'
currentScope.$emit('$includeContentLoaded');
scope.$eval(onloadExp);
}).error(function() {
if (thisChangeId === changeCounter) cleanupLastIncludeContent();
if (thisChangeId === changeCounter) {
cleanupLastIncludeContent();
scope.$emit('$includeContentError');
}
});
scope.$emit('$includeContentRequested');
} else {
Expand Down
23 changes: 23 additions & 0 deletions test/ng/directive/ngIncludeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ describe('ngInclude', function() {
}));


it('should fire $includeContentError event when content request fails', inject(
function($rootScope, $compile, $httpBackend, $templateCache) {
var contentLoadedSpy = jasmine.createSpy('content loaded'),
contentErrorSpy = jasmine.createSpy('content error');

$rootScope.$on('$includeContentLoaded', contentLoadedSpy);
$rootScope.$on('$includeContentError', contentErrorSpy);

$httpBackend.expect('GET', 'tpl.html').respond(400, 'nope');

element = $compile('<div><div ng-include="template"></div></div>')($rootScope);

$rootScope.$apply(function() {
$rootScope.template = 'tpl.html';
});
$httpBackend.flush();

expect(contentLoadedSpy).not.toHaveBeenCalled();
expect(contentErrorSpy).toHaveBeenCalledOnce();
expect(element.children('div').contents().length).toBe(0);
}));


it('should evaluate onload expression when a partial is loaded', inject(
putIntoCache('myUrl', 'my partial'),
function($rootScope, $compile) {
Expand Down