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

Commit b431ee3

Browse files
committed
fix($compile): fix replaceWith
the old implementation didn't reattach jquery/jqlite data which caused things like to be lost I tried various implementations but it appears that by reattaching the data to the new node by copying the expando property is the most reliable of all.
1 parent a44d3dc commit b431ee3

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

src/jqLite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
*/
7676

7777
var jqCache = {},
78-
jqName = 'ng-' + new Date().getTime(),
78+
jqName = JQLite.expando = 'ng-' + new Date().getTime(),
7979
jqId = 1,
8080
addEventListenerFn = (window.document.addEventListener
8181
? function(element, type, fn) {element.addEventListener(type, fn, false);}

src/ng/compile.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -867,22 +867,12 @@ function $CompileProvider($provide) {
867867
linkRootElement = linkQueue.pop(),
868868
origLinkNode = linkQueue.pop(),
869869
scope = linkQueue.pop(),
870-
linkNode = compileNode,
871-
$origLinkNode = jqLite(origLinkNode);
870+
linkNode = compileNode;
872871

873872
if (origLinkNode !== origCompileNode) {
874873
// it was cloned therefore we have to clone as well.
875874
linkNode = JQLiteClone(compileNode);
876-
replaceWith(linkRootElement, $origLinkNode, linkNode);
877-
}
878-
879-
if (replace) {
880-
if ($origLinkNode.data('$scope')) {
881-
// if the original element before replacement had a new scope, the replacement should
882-
// get it as well
883-
jqLite(linkNode).data('$scope', scope);
884-
}
885-
dealoc($origLinkNode);
875+
replaceWith(linkRootElement, jqLite(origLinkNode), linkNode);
886876
}
887877

888878
afterTemplateNodeLinkFn(function() {
@@ -992,12 +982,16 @@ function $CompileProvider($provide) {
992982
for(i = 0, ii = $rootElement.length; i < ii; i++) {
993983
if ($rootElement[i] == oldNode) {
994984
$rootElement[i] = newNode;
985+
break;
995986
}
996987
}
997988
}
989+
998990
if (parent) {
999991
parent.replaceChild(newNode, oldNode);
1000992
}
993+
994+
newNode[jqLite.expando] = oldNode[jqLite.expando];
1001995
$element[0] = newNode;
1002996
}
1003997
}];

0 commit comments

Comments
 (0)