Skip to content

Commit 92a57b2

Browse files
committed
fix($compile): retain CSS classes added in clone fn on async directive
In synchronous directives (without a templateUrl), classes added to the node during the clone attach function persist in the post-link function. Before this patch, classes addedd to asynchronous templates during the clone attach function would not persist after the node is replaced with the template prior to linking. Fixes angular#5439
1 parent c22ab5d commit 92a57b2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/ng/compile.js

+4
Original file line numberDiff line numberDiff line change
@@ -1711,9 +1711,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17111711
linkNode = $compileNode[0];
17121712

17131713
if (beforeTemplateLinkNode !== beforeTemplateCompileNode) {
1714+
var oldClasses = beforeTemplateLinkNode.className;
17141715
// it was cloned therefore we have to clone as well.
17151716
linkNode = jqLiteClone(compileNode);
17161717
replaceWith(linkRootElement, jqLite(beforeTemplateLinkNode), linkNode);
1718+
1719+
// Copy in CSS classes from original node
1720+
safeAddClass(jqLite(linkNode), oldClasses);
17171721
}
17181722
if (afterTemplateNodeLinkFn.transclude) {
17191723
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude);

test/ng/compileSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,26 @@ describe('$compile', function() {
11281128
});
11291129

11301130

1131+
it('should copy classes from pre-template node into linked element', function() {
1132+
module(function() {
1133+
directive('test', valueFn({
1134+
templateUrl: 'test.html',
1135+
replace: true
1136+
}));
1137+
});
1138+
inject(function($compile, $templateCache, $rootScope) {
1139+
var child;
1140+
$templateCache.put('test.html', '<p class="template-class">Hello</p>');
1141+
element = $compile('<div test></div>')($rootScope, function(node) {
1142+
node.addClass('clonefn-class');
1143+
});
1144+
$rootScope.$digest();
1145+
expect(element).toHaveClass('template-class');
1146+
expect(element).toHaveClass('clonefn-class');
1147+
});
1148+
});
1149+
1150+
11311151
describe('delay compile / linking functions until after template is resolved', function(){
11321152
var template;
11331153
beforeEach(module(function() {

0 commit comments

Comments
 (0)