Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($compile): retain CSS classes added in cloneAttachFn on asynchron…
Browse files Browse the repository at this point in the history
…ous directives

Previously, classes added to asynchronous directive elements during the clone
attach function would not persist after the node is merged with the template, prior
to linking. This change corrects this behaviour and brings it in line with synchronous
directives.

Closes #5439
Closes #5617
  • Loading branch information
caitp committed Jan 31, 2014
1 parent c22ab5d commit 5ed721b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1711,9 +1711,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
linkNode = $compileNode[0];

if (beforeTemplateLinkNode !== beforeTemplateCompileNode) {
var oldClasses = beforeTemplateLinkNode.className;
// it was cloned therefore we have to clone as well.
linkNode = jqLiteClone(compileNode);
replaceWith(linkRootElement, jqLite(beforeTemplateLinkNode), linkNode);

// Copy in CSS classes from original node
safeAddClass(jqLite(linkNode), oldClasses);
}
if (afterTemplateNodeLinkFn.transclude) {
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude);
Expand Down
20 changes: 20 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,26 @@ describe('$compile', function() {
});


it('should copy classes from pre-template node into linked element', function() {
module(function() {
directive('test', valueFn({
templateUrl: 'test.html',
replace: true
}));
});
inject(function($compile, $templateCache, $rootScope) {
var child;
$templateCache.put('test.html', '<p class="template-class">Hello</p>');
element = $compile('<div test></div>')($rootScope, function(node) {
node.addClass('clonefn-class');
});
$rootScope.$digest();
expect(element).toHaveClass('template-class');
expect(element).toHaveClass('clonefn-class');
});
});


describe('delay compile / linking functions until after template is resolved', function(){
var template;
beforeEach(module(function() {
Expand Down

0 comments on commit 5ed721b

Please sign in to comment.