fix($compile): replaced element has isolate scope

This commit is contained in:
Misko Hevery 2013-11-05 15:50:53 -08:00 committed by Igor Minar
parent d0efd5eefc
commit 97c7a4e379
2 changed files with 98 additions and 14 deletions

View file

@ -1237,16 +1237,16 @@ function $CompileProvider($provide) {
// combine directives from the original node and from the template:
// - take the array of directives for this element
// - split it into two parts, those that were already applied and those that weren't
// - collect directives from the template, add them to the second group and sort them
// - append the second group with new directives to the first group
directives = directives.concat(
collectDirectives(
compileNode,
directives.splice(i + 1, directives.length - (i + 1)),
newTemplateAttrs
)
);
// - split it into two parts, those that already applied (processed) and those that weren't (unprocessed)
// - collect directives from the template and sort them by priority
// - combine directives as: processed + template + unprocessed
var templateDirectives = collectDirectives(compileNode, [], newTemplateAttrs);
var unprocessedDirectives = directives.splice(i + 1, directives.length - (i + 1));
if (newIsolateScopeDirective) {
markDirectivesAsIsolate(templateDirectives);
}
directives = directives.concat(templateDirectives).concat(unprocessedDirectives);
mergeTemplateAttributes(templateAttrs, newTemplateAttrs);
ii = directives.length;
@ -1303,13 +1303,13 @@ function $CompileProvider($provide) {
if (pre) {
if (attrStart) pre = groupElementsLinkFnWrapper(pre, attrStart, attrEnd);
pre.require = directive.require;
if (newIsolateScopeDirective === directive) pre.isolateScope = true;
if (newIsolateScopeDirective === directive || directive.$$isolateScope) pre.isolateScope = true;
preLinkFns.push(pre);
}
if (post) {
if (attrStart) post = groupElementsLinkFnWrapper(post, attrStart, attrEnd);
post.require = directive.require;
if (newIsolateScopeDirective === directive) post.isolateScope = true;
if (newIsolateScopeDirective === directive || directive.$$isolateScope) post.isolateScope = true;
postLinkFns.push(post);
}
}
@ -1501,6 +1501,12 @@ function $CompileProvider($provide) {
}
}
function markDirectivesAsIsolate(directives) {
// mark all directives as needing isolate scope.
for (var j = 0, jj = directives.length; j < jj; j++) {
directives[j] = inherit(directives[j], {$$isolateScope: true});
}
}
/**
* looks up the directive and decorates it with exception handling and proper parameters. We
@ -1616,7 +1622,12 @@ function $CompileProvider($provide) {
tempTemplateAttrs = {$attr: {}};
replaceWith($rootElement, $compileNode, compileNode);
collectDirectives(compileNode, directives, tempTemplateAttrs);
var templateDirectives = collectDirectives(compileNode, [], tempTemplateAttrs);
if (isObject(origAsyncDirective.scope)) {
markDirectivesAsIsolate(templateDirectives);
}
directives = templateDirectives.concat(directives);
mergeTemplateAttributes(tAttrs, tempTemplateAttrs);
} else {
compileNode = beforeTemplateCompileNode;

View file

@ -2450,7 +2450,7 @@ describe('$compile', function() {
it('should require controller of an isolate directive from a non-isolate directive on the ' +
'same element', function() {
'same element', function() {
var IsolateController = function() {};
var isolateDirControllerInNonIsolateDirective;
@ -2480,6 +2480,79 @@ describe('$compile', function() {
});
it('should share isolate scope with replaced directives', function() {
var normalScope;
var isolateScope;
module(function() {
directive('isolate', function() {
return {
replace: true,
scope: {},
template: '<span ng-init="name=\'WORKS\'">{{name}}</span>',
link: function(s) {
isolateScope = s;
}
};
});
directive('nonIsolate', function() {
return {
link: function(s) {
normalScope = s;
}
};
});
});
inject(function($compile, $rootScope) {
element = $compile('<div isolate non-isolate></div>')($rootScope);
expect(normalScope).toBe($rootScope);
expect(normalScope.name).toEqual(undefined);
expect(isolateScope.name).toEqual('WORKS');
$rootScope.$digest();
expect(element.text()).toEqual('WORKS');
});
});
it('should share isolate scope with replaced directives', function() {
var normalScope;
var isolateScope;
module(function() {
directive('isolate', function() {
return {
replace: true,
scope: {},
templateUrl: 'main.html',
link: function(s) {
isolateScope = s;
}
};
});
directive('nonIsolate', function() {
return {
link: function(s) {
normalScope = s;
}
};
});
});
inject(function($compile, $rootScope, $templateCache) {
$templateCache.put('main.html', '<span ng-init="name=\'WORKS\'">{{name}}</span>');
element = $compile('<div isolate non-isolate></div>')($rootScope);
$rootScope.$apply();
expect(normalScope).toBe($rootScope);
expect(normalScope.name).toEqual(undefined);
expect(isolateScope.name).toEqual('WORKS');
expect(element.text()).toEqual('WORKS');
});
});
it('should require controller of a non-isolate directive from an isolate directive on the ' +
'same element', function() {
var NonIsolateController = function() {};