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

Commit 18ae985

Browse files
committed
fix($compile): don't instantiate controllers twice for element transclude directives
This is a fix for regression introduced last week by faf5b98. Closes #4654
1 parent 797c99e commit 18ae985

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/ng/compile.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,13 @@ function $CompileProvider($provide) {
11911191

11921192
childTranscludeFn = compile($template, transcludeFn, terminalPriority,
11931193
replaceDirective && replaceDirective.name, {
1194-
controllerDirectives: controllerDirectives,
1195-
newIsolateScopeDirective: newIsolateScopeDirective,
1196-
templateDirective: templateDirective,
1194+
// Don't pass in:
1195+
// - controllerDirectives - otherwise we'll create duplicates controllers
1196+
// - newIsolateScopeDirective or templateDirective - combining templates with
1197+
// element transclusion doesn't make sense.
1198+
//
1199+
// We need only transcludeDirective so that we prevent putting transclusion
1200+
// on the same element more than once.
11971201
transcludeDirective: transcludeDirective
11981202
});
11991203
} else {

test/ng/compileSpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -3179,6 +3179,47 @@ describe('$compile', function() {
31793179
expect(log).toEqual('compile:elementTrans; compile:regularTrans; regular');
31803180
});
31813181
});
3182+
3183+
3184+
it('should instantiate high priority controllers only once, but low priority ones each time we transclude',
3185+
function() {
3186+
module(function() {
3187+
directive('elementTrans', function(log) {
3188+
return {
3189+
transclude: 'element',
3190+
priority: 50,
3191+
controller: function($transclude, $element) {
3192+
log('controller:elementTrans');
3193+
$transclude(function(clone) {
3194+
$element.after(clone);
3195+
});
3196+
$transclude(function(clone) {
3197+
$element.after(clone);
3198+
});
3199+
$transclude(function(clone) {
3200+
$element.after(clone);
3201+
});
3202+
}
3203+
};
3204+
});
3205+
directive('normalDir', function(log) {
3206+
return {
3207+
controller: function() {
3208+
log('controller:normalDir');
3209+
}
3210+
};
3211+
});
3212+
});
3213+
inject(function($compile, $rootScope, log) {
3214+
element = $compile('<div><div element-trans normal-dir></div></div>')($rootScope);
3215+
expect(log).toEqual([
3216+
'controller:elementTrans',
3217+
'controller:normalDir',
3218+
'controller:normalDir',
3219+
'controller:normalDir'
3220+
]);
3221+
});
3222+
});
31823223
});
31833224

31843225

0 commit comments

Comments
 (0)