mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-08 23:04:45 +00:00
fix(ngInclude): ensure ngInclude is terminal and uses its own manual transclusion system
This commit is contained in:
parent
45dc9ee7b4
commit
1b5bee4fa1
2 changed files with 44 additions and 16 deletions
|
|
@ -149,18 +149,23 @@
|
||||||
* @description
|
* @description
|
||||||
* Emitted every time the ngInclude content is reloaded.
|
* Emitted every time the ngInclude content is reloaded.
|
||||||
*/
|
*/
|
||||||
|
var NG_INCLUDE_PRIORITY = 500;
|
||||||
var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile', '$animate', '$sce',
|
var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile', '$animate', '$sce',
|
||||||
function($http, $templateCache, $anchorScroll, $compile, $animate, $sce) {
|
function($http, $templateCache, $anchorScroll, $compile, $animate, $sce) {
|
||||||
return {
|
return {
|
||||||
restrict: 'ECA',
|
restrict: 'ECA',
|
||||||
terminal: true,
|
terminal: true,
|
||||||
transclude: 'element',
|
priority: NG_INCLUDE_PRIORITY,
|
||||||
compile: function(element, attr, transclusion) {
|
compile: function(element, attr) {
|
||||||
var srcExp = attr.ngInclude || attr.src,
|
var srcExp = attr.ngInclude || attr.src,
|
||||||
onloadExp = attr.onload || '',
|
onloadExp = attr.onload || '',
|
||||||
autoScrollExp = attr.autoscroll;
|
autoScrollExp = attr.autoscroll;
|
||||||
|
|
||||||
return function(scope, $element) {
|
element.html('');
|
||||||
|
var anchor = jqLite(document.createComment(' ngInclude: ' + srcExp + ' '));
|
||||||
|
element.replaceWith(anchor);
|
||||||
|
|
||||||
|
return function(scope) {
|
||||||
var changeCounter = 0,
|
var changeCounter = 0,
|
||||||
currentScope,
|
currentScope,
|
||||||
currentElement;
|
currentElement;
|
||||||
|
|
@ -184,23 +189,21 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
|
||||||
if (thisChangeId !== changeCounter) return;
|
if (thisChangeId !== changeCounter) return;
|
||||||
var newScope = scope.$new();
|
var newScope = scope.$new();
|
||||||
|
|
||||||
transclusion(newScope, function(clone) {
|
cleanupLastIncludeContent();
|
||||||
cleanupLastIncludeContent();
|
|
||||||
|
|
||||||
currentScope = newScope;
|
currentScope = newScope;
|
||||||
currentElement = clone;
|
currentElement = element.clone();
|
||||||
|
currentElement.html(response);
|
||||||
|
$animate.enter(currentElement, null, anchor);
|
||||||
|
|
||||||
currentElement.html(response);
|
$compile(currentElement, false, NG_INCLUDE_PRIORITY - 1)(currentScope);
|
||||||
$animate.enter(currentElement, null, $element);
|
|
||||||
$compile(currentElement.contents())(currentScope);
|
|
||||||
|
|
||||||
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
|
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
|
||||||
$anchorScroll();
|
$anchorScroll();
|
||||||
}
|
}
|
||||||
|
|
||||||
currentScope.$emit('$includeContentLoaded');
|
currentScope.$emit('$includeContentLoaded');
|
||||||
scope.$eval(onloadExp);
|
scope.$eval(onloadExp);
|
||||||
});
|
|
||||||
}).error(function() {
|
}).error(function() {
|
||||||
if (thisChangeId === changeCounter) cleanupLastIncludeContent();
|
if (thisChangeId === changeCounter) cleanupLastIncludeContent();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -280,6 +280,31 @@ describe('ngInclude', function() {
|
||||||
dealoc(element);
|
dealoc(element);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it('should compile only the inner content once', function() {
|
||||||
|
var log = [];
|
||||||
|
|
||||||
|
module(function($compileProvider) {
|
||||||
|
$compileProvider.directive('compileLog', function() {
|
||||||
|
return {
|
||||||
|
compile: function() {
|
||||||
|
log.push('compile');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
inject(function($compile, $rootScope, $templateCache) {
|
||||||
|
$templateCache.put('tpl.html', [200, '<div compile-log>123</div>', {}]);
|
||||||
|
element = $compile('<div><div ng-include="exp"></div></div>')($rootScope);
|
||||||
|
|
||||||
|
$rootScope.exp = 'tpl.html';
|
||||||
|
$rootScope.$digest();
|
||||||
|
|
||||||
|
expect(element.text()).toBe('123');
|
||||||
|
expect(log).toEqual(['compile']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('autoscoll', function() {
|
describe('autoscoll', function() {
|
||||||
var autoScrollSpy;
|
var autoScrollSpy;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue