Skip to content

Commit 462d709

Browse files
tboschjamesdaily
authored andcommitted
fix(ngInclude): Don't throw when the ngInclude element contains content with directives.
Related to angular#5069
1 parent 4a65179 commit 462d709

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/ng/directive/ngInclude.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,23 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
188188
if (thisChangeId !== changeCounter) return;
189189
var newScope = scope.$new();
190190

191-
$transclude(newScope, function(clone) {
192-
cleanupLastIncludeContent();
193-
194-
currentScope = newScope;
195-
currentElement = clone;
196-
197-
currentElement.html(response);
198-
$animate.enter(currentElement, null, $element, afterAnimation);
199-
$compile(currentElement.contents())(currentScope);
200-
currentScope.$emit('$includeContentLoaded');
201-
scope.$eval(onloadExp);
202-
});
191+
// Note: This will also link all children of ng-include that were contained in the original
192+
// html. If that content contains controllers, ... they could pollute/change the scope.
193+
// However, using ng-include on an element with additional content does not make sense...
194+
// Note: We can't remove them in the cloneAttchFn of $transclude as that
195+
// function is called before linking the content, which would apply child
196+
// directives to non existing elements.
197+
var clone = $transclude(newScope, noop);
198+
cleanupLastIncludeContent();
199+
200+
currentScope = newScope;
201+
currentElement = clone;
202+
203+
currentElement.html(response);
204+
$animate.enter(currentElement, null, $element, afterAnimation);
205+
$compile(currentElement.contents())(currentScope);
206+
currentScope.$emit('$includeContentLoaded');
207+
scope.$eval(onloadExp);
203208
}).error(function() {
204209
if (thisChangeId === changeCounter) cleanupLastIncludeContent();
205210
});

test/ng/directive/ngIncludeSpec.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,22 @@ describe('ngInclude', function() {
465465
});
466466

467467
describe('ngInclude and transcludes', function() {
468+
var element, directive;
469+
470+
beforeEach(module(function($compileProvider) {
471+
element = null;
472+
directive = $compileProvider.directive;
473+
}));
474+
475+
afterEach(function() {
476+
if (element) {
477+
dealoc(element);
478+
}
479+
});
480+
468481
it('should allow access to directive controller from children when used in a replace template', function() {
469482
var controller;
470-
module(function($compileProvider) {
471-
var directive = $compileProvider.directive;
483+
module(function() {
472484
directive('template', valueFn({
473485
template: '<div ng-include="\'include.html\'"></div>',
474486
replace: true,
@@ -485,13 +497,33 @@ describe('ngInclude and transcludes', function() {
485497
});
486498
inject(function($compile, $rootScope, $httpBackend) {
487499
$httpBackend.expectGET('include.html').respond('<div><div test></div></div>');
488-
var element = $compile('<div><div template></div></div>')($rootScope);
500+
element = $compile('<div><div template></div></div>')($rootScope);
489501
$rootScope.$apply();
490502
$httpBackend.flush();
491503
expect(controller.flag).toBe(true);
492-
dealoc(element);
493504
});
494505
});
506+
507+
it("should compile it's content correctly (although we remove it later)", function() {
508+
var testElement;
509+
module(function() {
510+
directive('test', function() {
511+
return {
512+
link: function(scope, element) {
513+
testElement = element;
514+
}
515+
};
516+
});
517+
});
518+
inject(function($compile, $rootScope, $httpBackend) {
519+
$httpBackend.expectGET('include.html').respond(' ');
520+
element = $compile('<div><div ng-include="\'include.html\'"><div test></div></div></div>')($rootScope);
521+
$rootScope.$apply();
522+
$httpBackend.flush();
523+
expect(testElement[0].nodeName).toBe('DIV');
524+
});
525+
526+
});
495527
});
496528

497529
describe('ngInclude animations', function() {

0 commit comments

Comments
 (0)