fix(ng-include): Compile only content

This commit is contained in:
Vojta Jina 2012-03-04 00:53:45 -08:00
parent 4f797fe5f3
commit c2989f6cc6
2 changed files with 56 additions and 40 deletions

View file

@ -69,51 +69,49 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
var srcExp = attr.src,
scopeExp = attr.scope || '',
autoScrollExp = attr.autoscroll;
if (!element[0]['ng:compiled']) {
element[0]['ng:compiled'] = true;
return function(scope, element, attr){
var changeCounter = 0,
childScope;
function incrementChange() { changeCounter++;}
scope.$watch(srcExp, incrementChange);
scope.$watch(function() {
var includeScope = scope.$eval(scopeExp);
if (includeScope) return includeScope.$id;
}, incrementChange);
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
var src = scope.$eval(srcExp),
useScope = scope.$eval(scopeExp);
return function(scope, element, attr) {
var changeCounter = 0,
childScope;
function clearContent() {
// if this callback is still desired
if (newChangeCounter === changeCounter) {
if (childScope) childScope.$destroy();
childScope = null;
element.html('');
}
function incrementChange() { changeCounter++;}
scope.$watch(srcExp, incrementChange);
scope.$watch(function() {
var includeScope = scope.$eval(scopeExp);
if (includeScope) return includeScope.$id;
}, incrementChange);
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
var src = scope.$eval(srcExp),
useScope = scope.$eval(scopeExp);
function clearContent() {
// if this callback is still desired
if (newChangeCounter === changeCounter) {
if (childScope) childScope.$destroy();
childScope = null;
element.html('');
}
}
if (src) {
$http.get(src, {cache: $templateCache}).success(function(response) {
// if this callback is still desired
if (newChangeCounter === changeCounter) {
element.html(response);
if (childScope) childScope.$destroy();
childScope = useScope ? useScope : scope.$new();
$compile(element)(childScope);
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
$anchorScroll();
}
scope.$emit('$contentLoaded');
if (src) {
$http.get(src, {cache: $templateCache}).success(function(response) {
// if this callback is still desired
if (newChangeCounter === changeCounter) {
element.html(response);
if (childScope) childScope.$destroy();
childScope = useScope ? useScope : scope.$new();
$compile(element.contents())(childScope);
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
$anchorScroll();
}
}).error(clearContent);
} else {
clearContent();
}
});
};
}
scope.$emit('$contentLoaded');
}
}).error(clearContent);
} else {
clearContent();
}
});
};
}
}
}];

View file

@ -245,6 +245,24 @@ describe('widget', function() {
}));
it('should compile only the content', inject(function($compile, $rootScope, $templateCache) {
// regression
var onload = jasmine.createSpy('$contentLoaded');
$rootScope.$on('$contentLoaded', onload);
$templateCache.put('tpl.html', [200, 'partial {{tpl}}', {}]);
element = $compile('<div><div ng:repeat="i in [1]">' +
'<ng:include src="tpl"></ng:include></div></div>')($rootScope);
expect(onload).not.toHaveBeenCalled();
$rootScope.$apply(function() {
$rootScope.tpl = 'tpl.html';
});
expect(onload).toHaveBeenCalledOnce();
}));
describe('autoscoll', function() {
var autoScrollSpy;