Skip to content

Commit

Permalink
fix($compile): do not double bound transclude functions
Browse files Browse the repository at this point in the history
Do not double bound transclude functions that are passed to the
compiler that are already bounded

Closes angular#9413
  • Loading branch information
lgalfaso committed Oct 12, 2014
1 parent b6f4d4b commit 9300ed2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

function createBoundTranscludeFn(scope, transcludeFn, previousBoundTranscludeFn, elementTransclusion) {

var boundTranscludeFn = function(transcludedScope, cloneFn, controllers, futureParentElement, containingScope) {
var boundTranscludeFn = function(transcludedScope, cloneFn, futureParentElement, controllers, containingScope) {

if (!transcludedScope) {
if (!transcludedScope && !transcludeFn.$$bound) {
transcludedScope = scope.$new(false, containingScope);
transcludedScope.$$transcluded = true;
}
Expand Down Expand Up @@ -1793,7 +1793,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
isolateScope = scope.$new(true);
}

transcludeFn = boundTranscludeFn && controllersBoundTransclude;
if (boundTranscludeFn) {
transcludeFn = controllersBoundTransclude;
transcludeFn.$$bound = true;
}
if (controllerDirectives) {
// TODO: merge `controllers` and `elementControllers` into single object.
controllers = {};
Expand Down Expand Up @@ -1953,7 +1956,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
var transcludeControllers;

// No scope passed in:
if (!isScope(scope)) {
if (!isScope(scope) && !isUndefined(scope)) {
futureParentElement = cloneAttachFn;
cloneAttachFn = scope;
scope = undefined;
Expand All @@ -1965,7 +1968,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (!futureParentElement) {
futureParentElement = hasElementTranscludeDirective ? $element.parent() : $element;
}
return boundTranscludeFn(scope, cloneAttachFn, transcludeControllers, futureParentElement, scopeToChild);
return boundTranscludeFn(scope, cloneAttachFn, futureParentElement, transcludeControllers, scopeToChild);
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5124,6 +5124,39 @@ describe('$compile', function() {
});


it('should bind the tranclude function to the original scope when used ' +
'in a future `$compile` call', function() {
module(function() {
directive('usesTransclude', function($compile) {
return {
scope: {},
transclude: true,
template: '<div><div ng-transclude></div></div>',
compile: function(tElement, tAttrs) {
var content = tElement.contents();
tElement.empty();
return function(scope, element, attrs, ctrls, transcludeFn) {
element.append(content);
$compile(content, transcludeFn)(scope);
};
}
};
});
});
inject(function($compile) {
element = $compile(
'<div>' +
'<div ng-init="outer=true"></div>' +
'<div uses-transclude>' +
'<span ng-if="outer">Success</span><span ng-if="!outer">Error</span>' +
'</div>' +
'</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Success');
});
});


// see issue https://github.com/angular/angular.js/issues/9095
describe('removing a transcluded element', function() {

Expand Down

0 comments on commit 9300ed2

Please sign in to comment.