Skip to content

Commit 64bd867

Browse files
committed
fix($compile): copy expandostore data into replaced nodes
Before this patch, with modern versions of jQuery being used, the element data cache was not being copied over into the new element when replacing an asynchronous directive's compile node with its template. This caused an issue where the element passed into a post link function's scope() method would return the incorrect scope. The test which is added passes regardless of the change in jQuery 1.10.2, which is currently checked into the tree, but fails without the change in jQuery 2.0.3. Closes angular#5099
1 parent b6c42d5 commit 64bd867

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/ng/compile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18641864
var firstElementToRemove = elementsToRemove[0],
18651865
removeCount = elementsToRemove.length,
18661866
parent = firstElementToRemove.parentNode,
1867-
i, ii;
1867+
i, ii, data;
1868+
1869+
data = elementsToRemove.data();
18681870

18691871
if ($rootElement) {
18701872
for(i = 0, ii = $rootElement.length; i < ii; i++) {
@@ -1900,6 +1902,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19001902

19011903
elementsToRemove[0] = newNode;
19021904
elementsToRemove.length = 1;
1905+
1906+
elementsToRemove.data(data);
19031907
}
19041908

19051909

test/ng/compileSpec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,31 @@ describe('$compile', function() {
13911391
expect(element.html()).toContain('i = 1');
13921392
});
13931393
});
1394+
1395+
it('should copy compileNode data into linkNode\'s data', function() {
1396+
module(function($compileProvider) {
1397+
$compileProvider.directive('tplDir', function(log) {
1398+
return {
1399+
scope: true,
1400+
templateUrl: 'tplDir.html',
1401+
replace: true,
1402+
link: function(scope, element) {
1403+
log(scope.$id);
1404+
log(element.scope().$id);
1405+
}
1406+
};
1407+
});
1408+
});
1409+
inject(function($compile, $rootScope, $templateCache, log) {
1410+
$templateCache.put('tplDir.html', '<div></div>');
1411+
element = $compile('<div tpl-dir></div>')($rootScope);
1412+
$rootScope.$digest();
1413+
var $log = log.toArray();
1414+
expect($log.length).toBe(2);
1415+
console.log($log[0], $log[1]);
1416+
expect($log[0]).toBe($log[1]);
1417+
});
1418+
});
13941419
});
13951420

13961421

0 commit comments

Comments
 (0)