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

Commit

Permalink
fix($compile): replaced element has isolate scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery authored and IgorMinar committed Nov 8, 2013
1 parent d0efd5e commit 97c7a4e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
37 changes: 24 additions & 13 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
75 changes: 74 additions & 1 deletion test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {};
Expand Down

0 comments on commit 97c7a4e

Please sign in to comment.