diff --git a/src/ng/compile.js b/src/ng/compile.js
index fd3c228e3de7..80006e5cec7b 100644
--- a/src/ng/compile.js
+++ b/src/ng/compile.js
@@ -1047,27 +1047,28 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
maxPriority, ignoreDirective, previousCompileContext);
compile.$$addScopeClass($compileNodes);
var namespace = null;
- var namespaceAdaptedCompileNodes = $compileNodes;
- var lastCompileNode;
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers, parentBoundTranscludeFn, futureParentElement){
assertArg(scope, 'scope');
if (!namespace) {
namespace = detectNamespaceForChildElements(futureParentElement);
}
- if (namespace !== 'html' && $compileNodes[0] !== lastCompileNode) {
- namespaceAdaptedCompileNodes = jqLite(
+ var $linkNode;
+ if (namespace !== 'html') {
+ // When using a directive with replace:true and templateUrl the $compileNodes
+ // (or a child element inside of them)
+ // might change, so we need to recreate the namespace adapted compileNodes
+ // for call to the link function.
+ // Note: This will already clone the nodes...
+ $linkNode = jqLite(
wrapTemplate(namespace, jqLite('
').append($compileNodes).html())
);
+ } else if (cloneConnectFn) {
+ // important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart
+ // and sometimes changes the structure of the DOM.
+ $linkNode = JQLitePrototype.clone.call($compileNodes);
+ } else {
+ $linkNode = $compileNodes;
}
- // When using a directive with replace:true and templateUrl the $compileNodes
- // might change, so we need to recreate the namespace adapted compileNodes.
- lastCompileNode = $compileNodes[0];
-
- // important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart
- // and sometimes changes the structure of the DOM.
- var $linkNode = cloneConnectFn
- ? JQLitePrototype.clone.call(namespaceAdaptedCompileNodes) // IMPORTANT!!!
- : namespaceAdaptedCompileNodes;
if (transcludeControllers) {
for (var controllerName in transcludeControllers) {
diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js
index 816aa4a7b503..5f44fa3ddc7f 100755
--- a/test/ng/compileSpec.js
+++ b/test/ng/compileSpec.js
@@ -300,7 +300,7 @@ describe('$compile', function() {
});
inject(function($compile, $rootScope, $httpBackend) {
$httpBackend.expect('GET', 'template.html').respond('');
- element = $compile('')($rootScope);
+ element = $compile('')($rootScope);
// initially the template is not yet loaded
$rootScope.$apply(function() {