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

Commit 45cc571

Browse files
committed
refactor($compile): throw when linking the same element more then once
1 parent 1591441 commit 45cc571

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/ng/compile.js

+7
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13091309
compile.$$addScopeClass($compileNodes);
13101310
var namespace = null;
13111311
return function publicLinkFn(scope, cloneConnectFn, options) {
1312+
if (!$compileNodes) {
1313+
throw $compileMinErr('multilink', 'This element has already been linked.');
1314+
}
13121315
assertArg(scope, 'scope');
13131316

13141317
if (previousCompileContext && previousCompileContext.needsNewScope) {
@@ -1363,6 +1366,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13631366

13641367
if (cloneConnectFn) cloneConnectFn($linkNode, scope);
13651368
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode, parentBoundTranscludeFn);
1369+
1370+
if (!cloneConnectFn) {
1371+
$compileNodes = compositeLinkFn = null;
1372+
}
13661373
return $linkNode;
13671374
};
13681375
}

test/ng/compileSpec.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,14 @@ describe('$compile', function() {
32303230
expect(element.text()).toBe('3');
32313231
});
32323232
});
3233+
3234+
it('should throw multilink error when linking the same element more then once', function() {
3235+
var linker = $compile('<div>');
3236+
linker($rootScope).remove();
3237+
expect(function() {
3238+
linker($rootScope);
3239+
}).toThrowMinErr('$compile', 'multilink', 'This element has already been linked.');
3240+
});
32333241
});
32343242

32353243

@@ -6173,7 +6181,7 @@ describe('$compile', function() {
61736181
'</div>' +
61746182
'</div>' +
61756183
'</div>');
6176-
element = template($rootScope);
6184+
element = template($rootScope, noop);
61776185
$rootScope.$digest();
61786186
$timeout.flush();
61796187
$httpBackend.flush();
@@ -6183,10 +6191,11 @@ describe('$compile', function() {
61836191
$templateCache.removeAll();
61846192
var destroyedScope = $rootScope.$new();
61856193
destroyedScope.$destroy();
6186-
var clone = template(destroyedScope);
6194+
var clone = template(destroyedScope, noop);
61876195
$rootScope.$digest();
61886196
$timeout.flush();
61896197
expect(linkFn).not.toHaveBeenCalled();
6198+
clone.remove();
61906199
});
61916200
});
61926201

0 commit comments

Comments
 (0)