Skip to content

Commit 82a1c03

Browse files
committed
fix($compile): copy CSS classes from pre-template node to link node
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 0559652 commit 82a1c03

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/ng/compile.js

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

17091709
if (beforeTemplateLinkNode !== beforeTemplateCompileNode) {
1710+
var oldClasses = beforeTemplateLinkNode.className;
17101711
// it was cloned therefore we have to clone as well.
17111712
linkNode = jqLiteClone(compileNode);
17121713
replaceWith(linkRootElement, jqLite(beforeTemplateLinkNode), linkNode);
1714+
1715+
// Copy in CSS classes from original node
1716+
safeAddClass(jqLite(linkNode), oldClasses);
17131717
}
17141718
if (afterTemplateNodeLinkFn.transclude) {
17151719
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude);

test/ng/compileSpec.js

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

11301130

1131+
it('should copy classes from pre-template node into linked element', function() {
1132+
module(function() {
1133+
directive('outer', function($compile, $parse) {
1134+
return function(scope, element, attr) {
1135+
var template = $compile('<div inner></div>');
1136+
template(scope, function(node) {
1137+
element.append(node);
1138+
node.addClass('from-outer');
1139+
});
1140+
};
1141+
});
1142+
1143+
directive('inner', valueFn({
1144+
templateUrl: 'inner.html',
1145+
replace: true
1146+
}));
1147+
});
1148+
inject(function($compile, $templateCache, $rootScope) {
1149+
var child;
1150+
$templateCache.put('inner.html', '<p class="inner">Hello</p>');
1151+
element = $compile('<div outer></div>')($rootScope);
1152+
$rootScope.$digest();
1153+
child = element.children('p');
1154+
expect(child.length).toBe(1);
1155+
expect(child).toHaveClass('from-outer');
1156+
expect(child).toHaveClass('inner');
1157+
});
1158+
});
1159+
1160+
11311161
describe('delay compile / linking functions until after template is resolved', function(){
11321162
var template;
11331163
beforeEach(module(function() {

0 commit comments

Comments
 (0)