Skip to content

Commit efd736b

Browse files
maxmartIgorMinar
authored andcommitted
fix($compile): correctly merge class attr for replace directives
Merging of interpolated class attribute from directive template with replace:true works Closes angular#1006
1 parent a57141f commit efd736b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/ng/compile.js

+3
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ function $CompileProvider($provide) {
844844
var srcAttr = src.$attr,
845845
dstAttr = dst.$attr,
846846
$element = dst.$$element;
847+
847848
// reapply the old attributes to the new element
848849
forEach(dst, function(value, key) {
849850
if (key.charAt(0) != '$') {
@@ -853,10 +854,12 @@ function $CompileProvider($provide) {
853854
dst.$set(key, value, true, srcAttr[key]);
854855
}
855856
});
857+
856858
// copy the new attributes on the old attrs object
857859
forEach(src, function(value, key) {
858860
if (key == 'class') {
859861
safeAddClass($element, value);
862+
dst.class = (dst.class ? dst.class + ' ' : '') + value;
860863
} else if (key == 'style') {
861864
$element.attr('style', $element.attr('style') + ';' + value);
862865
} else if (key.charAt(0) != '$' && !dst.hasOwnProperty(key)) {

test/ng/compileSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ describe('$compile', function() {
378378
expect(element).toBe(attr.$$element);
379379
}
380380
}));
381+
$compileProvider.directive('replaceWithInterpolatedClass', valueFn({
382+
replace: true,
383+
template: '<div class="class_{{1+1}}">Replace with interpolated class!</div>',
384+
compile: function(element, attr) {
385+
attr.$set('compiled', 'COMPILED');
386+
expect(element).toBe(attr.$$element);
387+
}
388+
}));
381389
}));
382390

383391

@@ -456,6 +464,14 @@ describe('$compile', function() {
456464
}));
457465

458466

467+
it('should handle interpolated css from replacing directive', inject(
468+
function($compile, $rootScope) {
469+
element = $compile('<div replace-with-interpolated-class></div>')($rootScope);
470+
$rootScope.$digest();
471+
expect(element).toHaveClass('class_2');
472+
}));
473+
474+
459475
it('should merge interpolated css class', inject(function($compile, $rootScope) {
460476
element = $compile('<div class="one {{cls}} three" replace></div>')($rootScope);
461477

0 commit comments

Comments
 (0)