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

Commit d0efd5e

Browse files
vojtajinaIgorMinar
authored andcommitted
fix($compile): only pass isolate scope to children that belong to the isolate directive
I had to fix one unit test, as it assumed the broken behavior, where application template gets the isolate scope of other (isolate) directive, rather than the regular scope. BREAKING CHANGE: Child elements that are defined either in the application template or in some other directives template do not get the isolate scope. In theory, nobody should rely on this behavior, as it is very rare - in most cases the isolate directive has a template.
1 parent 909cabd commit d0efd5e

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/ng/compile.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1480,8 +1480,13 @@ function $CompileProvider($provide) {
14801480
}
14811481

14821482
// RECURSION
1483-
// TODO(vojta): only pass isolate if the isolate directive has template
1484-
childLinkFn && childLinkFn(isolateScope || scope, linkNode.childNodes, undefined, boundTranscludeFn);
1483+
// We only pass the isolate scope, if the isolate directive has a template,
1484+
// otherwise the child elements do not belong to the isolate directive.
1485+
var scopeToChild = scope;
1486+
if (newIsolateScopeDirective && (newIsolateScopeDirective.template || newIsolateScopeDirective.templateUrl === null)) {
1487+
scopeToChild = isolateScope;
1488+
}
1489+
childLinkFn && childLinkFn(scopeToChild, linkNode.childNodes, undefined, boundTranscludeFn);
14851490

14861491
// POSTLINKING
14871492
for(i = postLinkFns.length - 1; i >= 0; i--) {

test/ng/compileSpec.js

+58-2
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ describe('$compile', function() {
14261426
return {
14271427
restrict: 'CA',
14281428
link: {pre: function(scope) {
1429-
log('log-' + scope.$id + '-' + scope.$parent.$id);
1429+
log('log-' + scope.$id + '-' + (scope.$parent && scope.$parent.$id || 'no-parent'));
14301430
}}
14311431
};
14321432
});
@@ -1443,7 +1443,7 @@ describe('$compile', function() {
14431443
it('should allow creation of new isolated scopes for directives', inject(
14441444
function($rootScope, $compile, log) {
14451445
element = $compile('<div><span iscope><a log></a></span></div>')($rootScope);
1446-
expect(log).toEqual('log-002-001; LOG; 002');
1446+
expect(log).toEqual('log-001-no-parent; LOG; 002');
14471447
$rootScope.name = 'abc';
14481448
expect(iscope.$parent).toBe($rootScope);
14491449
expect(iscope.name).toBeUndefined();
@@ -2131,6 +2131,62 @@ describe('$compile', function() {
21312131
expect(componentScope.$parent).toBe(regularScope)
21322132
}));
21332133

2134+
it('should not give the isolate scope to other directive template', function() {
2135+
module(function() {
2136+
directive('otherTplDir', function() {
2137+
return {
2138+
template: 'value: {{value}}'
2139+
};
2140+
});
2141+
});
2142+
2143+
inject(function($rootScope) {
2144+
compile('<div my-component other-tpl-dir>');
2145+
2146+
$rootScope.$apply(function() {
2147+
$rootScope.value = 'from-parent';
2148+
});
2149+
2150+
expect(element.html()).toBe('value: from-parent');
2151+
});
2152+
});
2153+
2154+
2155+
it('should not give the isolate scope to other directive template (with templateUrl)', function() {
2156+
module(function() {
2157+
directive('otherTplDir', function() {
2158+
return {
2159+
templateUrl: 'other.html'
2160+
};
2161+
});
2162+
});
2163+
2164+
inject(function($rootScope, $templateCache) {
2165+
$templateCache.put('other.html', 'value: {{value}}')
2166+
compile('<div my-component other-tpl-dir>');
2167+
2168+
$rootScope.$apply(function() {
2169+
$rootScope.value = 'from-parent';
2170+
});
2171+
2172+
expect(element.html()).toBe('value: from-parent');
2173+
});
2174+
});
2175+
2176+
2177+
it('should not give the isolate scope to regular child elements', function() {
2178+
inject(function($rootScope) {
2179+
compile('<div my-component>value: {{value}}</div>');
2180+
2181+
$rootScope.$apply(function() {
2182+
$rootScope.value = 'from-parent';
2183+
});
2184+
2185+
expect(element.html()).toBe('value: from-parent');
2186+
});
2187+
});
2188+
2189+
21342190
describe('attribute', function() {
21352191
it('should copy simple attribute', inject(function() {
21362192
compile('<div><span my-component attr="some text">');

0 commit comments

Comments
 (0)