Skip to content

Commit 5b9dd4f

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 5b9dd4f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-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

+26
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,32 @@ 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+
$templateCache.put('inner.html', '<p class="inner">Hello</p>');
1150+
element = $compile('<div outer></div>')($rootScope);
1151+
$rootScope.$digest();
1152+
expect(element.children('p').eq(0)).toHaveClass(['from-outer', 'inner']);
1153+
});
1154+
});
1155+
1156+
11311157
describe('delay compile / linking functions until after template is resolved', function(){
11321158
var template;
11331159
beforeEach(module(function() {

0 commit comments

Comments
 (0)