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

Commit c2989f6

Browse files
committed
fix(ng-include): Compile only content
1 parent 4f797fe commit c2989f6

File tree

2 files changed

+58
-42
lines changed

2 files changed

+58
-42
lines changed

src/widgets.js

+40-42
Original file line numberDiff line numberDiff line change
@@ -69,51 +69,49 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
6969
var srcExp = attr.src,
7070
scopeExp = attr.scope || '',
7171
autoScrollExp = attr.autoscroll;
72-
if (!element[0]['ng:compiled']) {
73-
element[0]['ng:compiled'] = true;
74-
return function(scope, element, attr){
75-
var changeCounter = 0,
76-
childScope;
77-
78-
function incrementChange() { changeCounter++;}
79-
scope.$watch(srcExp, incrementChange);
80-
scope.$watch(function() {
81-
var includeScope = scope.$eval(scopeExp);
82-
if (includeScope) return includeScope.$id;
83-
}, incrementChange);
84-
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
85-
var src = scope.$eval(srcExp),
86-
useScope = scope.$eval(scopeExp);
87-
88-
function clearContent() {
89-
// if this callback is still desired
90-
if (newChangeCounter === changeCounter) {
91-
if (childScope) childScope.$destroy();
92-
childScope = null;
93-
element.html('');
94-
}
72+
73+
return function(scope, element, attr) {
74+
var changeCounter = 0,
75+
childScope;
76+
77+
function incrementChange() { changeCounter++;}
78+
scope.$watch(srcExp, incrementChange);
79+
scope.$watch(function() {
80+
var includeScope = scope.$eval(scopeExp);
81+
if (includeScope) return includeScope.$id;
82+
}, incrementChange);
83+
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
84+
var src = scope.$eval(srcExp),
85+
useScope = scope.$eval(scopeExp);
86+
87+
function clearContent() {
88+
// if this callback is still desired
89+
if (newChangeCounter === changeCounter) {
90+
if (childScope) childScope.$destroy();
91+
childScope = null;
92+
element.html('');
9593
}
94+
}
9695

97-
if (src) {
98-
$http.get(src, {cache: $templateCache}).success(function(response) {
99-
// if this callback is still desired
100-
if (newChangeCounter === changeCounter) {
101-
element.html(response);
102-
if (childScope) childScope.$destroy();
103-
childScope = useScope ? useScope : scope.$new();
104-
$compile(element)(childScope);
105-
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
106-
$anchorScroll();
107-
}
108-
scope.$emit('$contentLoaded');
96+
if (src) {
97+
$http.get(src, {cache: $templateCache}).success(function(response) {
98+
// if this callback is still desired
99+
if (newChangeCounter === changeCounter) {
100+
element.html(response);
101+
if (childScope) childScope.$destroy();
102+
childScope = useScope ? useScope : scope.$new();
103+
$compile(element.contents())(childScope);
104+
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
105+
$anchorScroll();
109106
}
110-
}).error(clearContent);
111-
} else {
112-
clearContent();
113-
}
114-
});
115-
};
116-
}
107+
scope.$emit('$contentLoaded');
108+
}
109+
}).error(clearContent);
110+
} else {
111+
clearContent();
112+
}
113+
});
114+
};
117115
}
118116
}
119117
}];

test/widgetsSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ describe('widget', function() {
245245
}));
246246

247247

248+
it('should compile only the content', inject(function($compile, $rootScope, $templateCache) {
249+
// regression
250+
251+
var onload = jasmine.createSpy('$contentLoaded');
252+
$rootScope.$on('$contentLoaded', onload);
253+
$templateCache.put('tpl.html', [200, 'partial {{tpl}}', {}]);
254+
255+
element = $compile('<div><div ng:repeat="i in [1]">' +
256+
'<ng:include src="tpl"></ng:include></div></div>')($rootScope);
257+
expect(onload).not.toHaveBeenCalled();
258+
259+
$rootScope.$apply(function() {
260+
$rootScope.tpl = 'tpl.html';
261+
});
262+
expect(onload).toHaveBeenCalledOnce();
263+
}));
264+
265+
248266
describe('autoscoll', function() {
249267
var autoScrollSpy;
250268

0 commit comments

Comments
 (0)