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

Commit 97c7a4e

Browse files
mheveryIgorMinar
authored andcommitted
fix($compile): replaced element has isolate scope
1 parent d0efd5e commit 97c7a4e

File tree

2 files changed

+98
-14
lines changed

2 files changed

+98
-14
lines changed

src/ng/compile.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -1237,16 +1237,16 @@ function $CompileProvider($provide) {
12371237

12381238
// combine directives from the original node and from the template:
12391239
// - take the array of directives for this element
1240-
// - split it into two parts, those that were already applied and those that weren't
1241-
// - collect directives from the template, add them to the second group and sort them
1242-
// - append the second group with new directives to the first group
1243-
directives = directives.concat(
1244-
collectDirectives(
1245-
compileNode,
1246-
directives.splice(i + 1, directives.length - (i + 1)),
1247-
newTemplateAttrs
1248-
)
1249-
);
1240+
// - split it into two parts, those that already applied (processed) and those that weren't (unprocessed)
1241+
// - collect directives from the template and sort them by priority
1242+
// - combine directives as: processed + template + unprocessed
1243+
var templateDirectives = collectDirectives(compileNode, [], newTemplateAttrs);
1244+
var unprocessedDirectives = directives.splice(i + 1, directives.length - (i + 1));
1245+
1246+
if (newIsolateScopeDirective) {
1247+
markDirectivesAsIsolate(templateDirectives);
1248+
}
1249+
directives = directives.concat(templateDirectives).concat(unprocessedDirectives);
12501250
mergeTemplateAttributes(templateAttrs, newTemplateAttrs);
12511251

12521252
ii = directives.length;
@@ -1303,13 +1303,13 @@ function $CompileProvider($provide) {
13031303
if (pre) {
13041304
if (attrStart) pre = groupElementsLinkFnWrapper(pre, attrStart, attrEnd);
13051305
pre.require = directive.require;
1306-
if (newIsolateScopeDirective === directive) pre.isolateScope = true;
1306+
if (newIsolateScopeDirective === directive || directive.$$isolateScope) pre.isolateScope = true;
13071307
preLinkFns.push(pre);
13081308
}
13091309
if (post) {
13101310
if (attrStart) post = groupElementsLinkFnWrapper(post, attrStart, attrEnd);
13111311
post.require = directive.require;
1312-
if (newIsolateScopeDirective === directive) post.isolateScope = true;
1312+
if (newIsolateScopeDirective === directive || directive.$$isolateScope) post.isolateScope = true;
13131313
postLinkFns.push(post);
13141314
}
13151315
}
@@ -1501,6 +1501,12 @@ function $CompileProvider($provide) {
15011501
}
15021502
}
15031503

1504+
function markDirectivesAsIsolate(directives) {
1505+
// mark all directives as needing isolate scope.
1506+
for (var j = 0, jj = directives.length; j < jj; j++) {
1507+
directives[j] = inherit(directives[j], {$$isolateScope: true});
1508+
}
1509+
}
15041510

15051511
/**
15061512
* looks up the directive and decorates it with exception handling and proper parameters. We
@@ -1616,7 +1622,12 @@ function $CompileProvider($provide) {
16161622

16171623
tempTemplateAttrs = {$attr: {}};
16181624
replaceWith($rootElement, $compileNode, compileNode);
1619-
collectDirectives(compileNode, directives, tempTemplateAttrs);
1625+
var templateDirectives = collectDirectives(compileNode, [], tempTemplateAttrs);
1626+
1627+
if (isObject(origAsyncDirective.scope)) {
1628+
markDirectivesAsIsolate(templateDirectives);
1629+
}
1630+
directives = templateDirectives.concat(directives);
16201631
mergeTemplateAttributes(tAttrs, tempTemplateAttrs);
16211632
} else {
16221633
compileNode = beforeTemplateCompileNode;

test/ng/compileSpec.js

+74-1
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,7 @@ describe('$compile', function() {
24502450

24512451

24522452
it('should require controller of an isolate directive from a non-isolate directive on the ' +
2453-
'same element', function() {
2453+
'same element', function() {
24542454
var IsolateController = function() {};
24552455
var isolateDirControllerInNonIsolateDirective;
24562456

@@ -2480,6 +2480,79 @@ describe('$compile', function() {
24802480
});
24812481

24822482

2483+
it('should share isolate scope with replaced directives', function() {
2484+
var normalScope;
2485+
var isolateScope;
2486+
2487+
module(function() {
2488+
directive('isolate', function() {
2489+
return {
2490+
replace: true,
2491+
scope: {},
2492+
template: '<span ng-init="name=\'WORKS\'">{{name}}</span>',
2493+
link: function(s) {
2494+
isolateScope = s;
2495+
}
2496+
};
2497+
});
2498+
directive('nonIsolate', function() {
2499+
return {
2500+
link: function(s) {
2501+
normalScope = s;
2502+
}
2503+
};
2504+
});
2505+
});
2506+
2507+
inject(function($compile, $rootScope) {
2508+
element = $compile('<div isolate non-isolate></div>')($rootScope);
2509+
2510+
expect(normalScope).toBe($rootScope);
2511+
expect(normalScope.name).toEqual(undefined);
2512+
expect(isolateScope.name).toEqual('WORKS');
2513+
$rootScope.$digest();
2514+
expect(element.text()).toEqual('WORKS');
2515+
});
2516+
});
2517+
2518+
2519+
it('should share isolate scope with replaced directives', function() {
2520+
var normalScope;
2521+
var isolateScope;
2522+
2523+
module(function() {
2524+
directive('isolate', function() {
2525+
return {
2526+
replace: true,
2527+
scope: {},
2528+
templateUrl: 'main.html',
2529+
link: function(s) {
2530+
isolateScope = s;
2531+
}
2532+
};
2533+
});
2534+
directive('nonIsolate', function() {
2535+
return {
2536+
link: function(s) {
2537+
normalScope = s;
2538+
}
2539+
};
2540+
});
2541+
});
2542+
2543+
inject(function($compile, $rootScope, $templateCache) {
2544+
$templateCache.put('main.html', '<span ng-init="name=\'WORKS\'">{{name}}</span>');
2545+
element = $compile('<div isolate non-isolate></div>')($rootScope);
2546+
$rootScope.$apply();
2547+
2548+
expect(normalScope).toBe($rootScope);
2549+
expect(normalScope.name).toEqual(undefined);
2550+
expect(isolateScope.name).toEqual('WORKS');
2551+
expect(element.text()).toEqual('WORKS');
2552+
});
2553+
});
2554+
2555+
24832556
it('should require controller of a non-isolate directive from an isolate directive on the ' +
24842557
'same element', function() {
24852558
var NonIsolateController = function() {};

0 commit comments

Comments
 (0)