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

Commit 56c6021

Browse files
committed
fix($compile): bound transclusion to correct scope
Nested isolated transclude directives. This improves/fixes the fix in d414b78. See the changed ng-ifunit test: The template inside ng-if should be bound to the isolate scope of `iso` directive (resp. its child scope). Not to a child of the root scope. This shows the issue with ng-if. It’s however problem with other directives too. Instead of remembering the scope, we pass around the bound parent transclusion.
1 parent 0c8a2cd commit 56c6021

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/ng/compile.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
847847
compileNodes($compileNodes, transcludeFn, $compileNodes,
848848
maxPriority, ignoreDirective, previousCompileContext);
849849
safeAddClass($compileNodes, 'ng-scope');
850-
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers){
850+
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers, parentBoundTranscludeFn){
851851
assertArg(scope, 'scope');
852852
// important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart
853853
// and sometimes changes the structure of the DOM.
@@ -869,7 +869,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
869869
}
870870

871871
if (cloneConnectFn) cloneConnectFn($linkNode, scope);
872-
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode);
872+
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode, parentBoundTranscludeFn);
873873
return $linkNode;
874874
};
875875
}
@@ -985,10 +985,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
985985

986986
function createBoundTranscludeFn(scope, transcludeFn, previousBoundTranscludeFn) {
987987

988-
// If there is a previous boundTransclude function and it has a transclusionScope then
989-
// use this instead of the current scope
990-
scope = previousBoundTranscludeFn && previousBoundTranscludeFn.transclusionScope || scope;
991-
992988
var boundTranscludeFn = function(transcludedScope, cloneFn, controllers) {
993989
var scopeCreated = false;
994990

@@ -998,16 +994,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
998994
scopeCreated = true;
999995
}
1000996

1001-
var clone = transcludeFn(transcludedScope, cloneFn, controllers);
997+
var clone = transcludeFn(transcludedScope, cloneFn, controllers, previousBoundTranscludeFn);
1002998
if (scopeCreated) {
1003999
clone.on('$destroy', function() { transcludedScope.$destroy(); });
10041000
}
10051001
return clone;
10061002
};
10071003

1008-
// Store the transclusionScope for nested transclusions
1009-
boundTranscludeFn.transclusionScope = scope;
1010-
10111004
return boundTranscludeFn;
10121005
}
10131006

@@ -1786,7 +1779,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17861779
// Copy in CSS classes from original node
17871780
safeAddClass(jqLite(linkNode), oldClasses);
17881781
}
1789-
if (afterTemplateNodeLinkFn.transclude) {
1782+
if (afterTemplateNodeLinkFn.transcludeOnThisElement) {
17901783
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn);
17911784
} else {
17921785
childBoundTranscludeFn = boundTranscludeFn;

test/ng/directive/ngIfSpec.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,20 @@ describe('ngIf and transcludes', function() {
204204
it('should use the correct transcluded scope', function() {
205205
module(function($compileProvider) {
206206
$compileProvider.directive('iso', valueFn({
207+
link: function(scope) {
208+
scope.val = 'value in iso scope';
209+
},
207210
restrict: 'E',
208211
transclude: true,
209-
template: '<div ng-if="true"><div ng-transclude></div></div>',
212+
template: '<div ng-if="true">val={{val}}-<div ng-transclude></div></div>',
210213
scope: {}
211214
}));
212215
});
213216
inject(function($compile, $rootScope) {
214217
$rootScope.val = 'transcluded content';
215218
var element = $compile('<iso><span ng-bind="val"></span></iso>')($rootScope);
216219
$rootScope.$digest();
217-
expect(trim(element.text())).toEqual('transcluded content');
220+
expect(trim(element.text())).toEqual('val=value in iso scope-transcluded content');
218221
dealoc(element);
219222
});
220223
});

0 commit comments

Comments
 (0)