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

Commit 1382d4e

Browse files
vojtajinacaitp
authored andcommitted
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. Conflicts: test/ng/directive/ngIfSpec.js
1 parent b9ddef2 commit 1382d4e

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/ng/compile.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
830830
compileNodes($compileNodes, transcludeFn, $compileNodes,
831831
maxPriority, ignoreDirective, previousCompileContext);
832832
safeAddClass($compileNodes, 'ng-scope');
833-
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers){
833+
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers, parentBoundTranscludeFn){
834834
assertArg(scope, 'scope');
835835
// important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart
836836
// and sometimes changes the structure of the DOM.
@@ -852,7 +852,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
852852
}
853853

854854
if (cloneConnectFn) cloneConnectFn($linkNode, scope);
855-
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode);
855+
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode, parentBoundTranscludeFn);
856856
return $linkNode;
857857
};
858858
}
@@ -968,10 +968,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
968968

969969
function createBoundTranscludeFn(scope, transcludeFn, previousBoundTranscludeFn) {
970970

971-
// If there is a previous boundTransclude function and it has a transclusionScope then
972-
// use this instead of the current scope
973-
scope = previousBoundTranscludeFn && previousBoundTranscludeFn.transclusionScope || scope;
974-
975971
var boundTranscludeFn = function(transcludedScope, cloneFn, controllers) {
976972
var scopeCreated = false;
977973

@@ -981,16 +977,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
981977
scopeCreated = true;
982978
}
983979

984-
var clone = transcludeFn(transcludedScope, cloneFn, controllers);
980+
var clone = transcludeFn(transcludedScope, cloneFn, controllers, previousBoundTranscludeFn);
985981
if (scopeCreated) {
986982
clone.on('$destroy', function() { transcludedScope.$destroy(); });
987983
}
988984
return clone;
989985
};
990986

991-
// Store the transclusionScope for nested transclusions
992-
boundTranscludeFn.transclusionScope = scope;
993-
994987
return boundTranscludeFn;
995988
}
996989

@@ -1767,7 +1760,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17671760
// Copy in CSS classes from original node
17681761
safeAddClass(jqLite(linkNode), oldClasses);
17691762
}
1770-
if (afterTemplateNodeLinkFn.transclude) {
1763+
if (afterTemplateNodeLinkFn.transcludeOnThisElement) {
17711764
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn);
17721765
} else {
17731766
childBoundTranscludeFn = boundTranscludeFn;

test/ng/directive/ngIfSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ describe('ngIf and transcludes', function() {
199199
dealoc(element);
200200
});
201201
});
202+
203+
204+
it('should use the correct transcluded scope', function() {
205+
module(function($compileProvider) {
206+
$compileProvider.directive('iso', valueFn({
207+
link: function(scope) {
208+
scope.val = 'value in iso scope';
209+
},
210+
restrict: 'E',
211+
transclude: true,
212+
template: '<div ng-if="true">val={{val}}-<div ng-transclude></div></div>',
213+
scope: {}
214+
}));
215+
});
216+
inject(function($compile, $rootScope) {
217+
$rootScope.val = 'transcluded content';
218+
var element = $compile('<iso><span ng-bind="val"></span></iso>')($rootScope);
219+
$rootScope.$digest();
220+
expect(trim(element.text())).toEqual('val=value in iso scope-transcluded content');
221+
dealoc(element);
222+
});
223+
});
202224
});
203225

204226
describe('ngIf animations', function () {

0 commit comments

Comments
 (0)