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

Commit 615f8a2

Browse files
committed
refactor($compile): throw when linking the same element more then once
1 parent 798fb18 commit 615f8a2

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/ng/compile.js

+7
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13321332
compile.$$addScopeClass($compileNodes);
13331333
var namespace = null;
13341334
return function publicLinkFn(scope, cloneConnectFn, options) {
1335+
if (!$compileNodes) {
1336+
throw $compileMinErr('multilink', 'This element has already been linked.');
1337+
}
13351338
assertArg(scope, 'scope');
13361339

13371340
if (previousCompileContext && previousCompileContext.needsNewScope) {
@@ -1386,6 +1389,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13861389

13871390
if (cloneConnectFn) cloneConnectFn($linkNode, scope);
13881391
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode, parentBoundTranscludeFn);
1392+
1393+
if (!cloneConnectFn) {
1394+
$compileNodes = compositeLinkFn = null;
1395+
}
13891396
return $linkNode;
13901397
};
13911398
}

test/ng/compileSpec.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,14 @@ describe('$compile', function() {
32383238
expect(element.text()).toBe('3');
32393239
});
32403240
});
3241+
3242+
it('should throw multilink error when linking the same element more then once', function() {
3243+
var linker = $compile('<div>');
3244+
linker($rootScope).remove();
3245+
expect(function() {
3246+
linker($rootScope);
3247+
}).toThrowMinErr('$compile', 'multilink', 'This element has already been linked.');
3248+
});
32413249
});
32423250

32433251

@@ -6022,6 +6030,22 @@ describe('$compile', function() {
60226030
});
60236031
});
60246032

6033+
it('should throw if a transcluded node is transcluded again', function() {
6034+
module(function() {
6035+
directive('trans', valueFn({
6036+
transclude: true,
6037+
link: function(scope, element, attr, ctrl, $transclude) {
6038+
$transclude();
6039+
$transclude();
6040+
}
6041+
}));
6042+
});
6043+
inject(function($rootScope, $compile) {
6044+
expect(function() {
6045+
$compile('<trans></trans>')($rootScope);
6046+
}).toThrowMinErr('$compile', 'multilink', 'This element has already been linked.');
6047+
});
6048+
});
60256049

60266050
it('should not leak if two "element" transclusions are on the same element (with debug info)', function() {
60276051
if (jQuery) {
@@ -6163,7 +6187,7 @@ describe('$compile', function() {
61636187
'</div>' +
61646188
'</div>' +
61656189
'</div>');
6166-
element = template($rootScope);
6190+
element = template($rootScope, noop);
61676191
$rootScope.$digest();
61686192
$timeout.flush();
61696193
$httpBackend.flush();
@@ -6173,10 +6197,11 @@ describe('$compile', function() {
61736197
$templateCache.removeAll();
61746198
var destroyedScope = $rootScope.$new();
61756199
destroyedScope.$destroy();
6176-
var clone = template(destroyedScope);
6200+
var clone = template(destroyedScope, noop);
61776201
$rootScope.$digest();
61786202
$timeout.flush();
61796203
expect(linkFn).not.toHaveBeenCalled();
6204+
clone.remove();
61806205
});
61816206
});
61826207

0 commit comments

Comments
 (0)