mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-15 18:23:09 +00:00
fix(ng-include): Compile only content
This commit is contained in:
parent
4f797fe5f3
commit
c2989f6cc6
2 changed files with 56 additions and 40 deletions
|
|
@ -69,51 +69,49 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
|
||||||
var srcExp = attr.src,
|
var srcExp = attr.src,
|
||||||
scopeExp = attr.scope || '',
|
scopeExp = attr.scope || '',
|
||||||
autoScrollExp = attr.autoscroll;
|
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++;}
|
return function(scope, element, attr) {
|
||||||
scope.$watch(srcExp, incrementChange);
|
var changeCounter = 0,
|
||||||
scope.$watch(function() {
|
childScope;
|
||||||
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() {
|
function incrementChange() { changeCounter++;}
|
||||||
// if this callback is still desired
|
scope.$watch(srcExp, incrementChange);
|
||||||
if (newChangeCounter === changeCounter) {
|
scope.$watch(function() {
|
||||||
if (childScope) childScope.$destroy();
|
var includeScope = scope.$eval(scopeExp);
|
||||||
childScope = null;
|
if (includeScope) return includeScope.$id;
|
||||||
element.html('');
|
}, 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) {
|
if (src) {
|
||||||
$http.get(src, {cache: $templateCache}).success(function(response) {
|
$http.get(src, {cache: $templateCache}).success(function(response) {
|
||||||
// if this callback is still desired
|
// if this callback is still desired
|
||||||
if (newChangeCounter === changeCounter) {
|
if (newChangeCounter === changeCounter) {
|
||||||
element.html(response);
|
element.html(response);
|
||||||
if (childScope) childScope.$destroy();
|
if (childScope) childScope.$destroy();
|
||||||
childScope = useScope ? useScope : scope.$new();
|
childScope = useScope ? useScope : scope.$new();
|
||||||
$compile(element)(childScope);
|
$compile(element.contents())(childScope);
|
||||||
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
|
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
|
||||||
$anchorScroll();
|
$anchorScroll();
|
||||||
}
|
|
||||||
scope.$emit('$contentLoaded');
|
|
||||||
}
|
}
|
||||||
}).error(clearContent);
|
scope.$emit('$contentLoaded');
|
||||||
} else {
|
}
|
||||||
clearContent();
|
}).error(clearContent);
|
||||||
}
|
} else {
|
||||||
});
|
clearContent();
|
||||||
};
|
}
|
||||||
}
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
describe('autoscoll', function() {
|
||||||
var autoScrollSpy;
|
var autoScrollSpy;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue