From 2501e950059b858c5e56803390c1a6c64ba9ad96 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Thu, 17 Jul 2014 20:30:28 -0700 Subject: [PATCH] fix($compile): updating the jQuery .context when it is replaced --- src/ng/compile.js | 5 +++++ test/ng/compileSpec.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/ng/compile.js b/src/ng/compile.js index 66677a3b5a80..b43ca265d6cf 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -2021,6 +2021,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { } } $rootElement.length -= removeCount - 1; + + //If the replaced element is also the jQuery .context then replace it + if ($rootElement.context === firstElementToRemove) { + $rootElement.context = newNode; + } break; } } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index aedff6e4d795..8cb8b5ae7a45 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -696,6 +696,29 @@ describe('$compile', function() { expect(child).toHaveClass('log'); // merged from replace directive template })); + it('should update references to replaced jQuery context', function() { + module(function($compileProvider) { + $compileProvider.directive('foo', function() { + return { + replace: true, + template: '
' + }; + }); + }); + + inject(function($compile, $rootScope) { + element = jqLite(document.createElement('span')).attr('foo', ''); + expect(nodeName_(element)).toBe('span'); + + var preCompiledNode = element[0]; + + var linked = $compile(element)($rootScope); + expect(linked).toBe(element); + expect(nodeName_(element)).toBe('div'); + expect(element.context).not.toBe(preCompiledNode); + }); + }); + it("should fail if replacing and template doesn't have a single root element", function() { module(function() { directive('noRootElem', function() {