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

Commit f49eaf8

Browse files
committed
fix($compile): Merge interpolated css class when replacing an element
1 parent f701ce0 commit f49eaf8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/service/compiler.js

+9
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ function $CompileProvider($provide) {
729729
// reapply the old attributes to the new element
730730
forEach(dst, function(value, key) {
731731
if (key.charAt(0) != '$') {
732+
if (src[key]) {
733+
value += (key === 'style' ? ';' : ' ') + src[key];
734+
}
732735
dst.$set(key, value, srcAttr[key]);
733736
}
734737
});
@@ -873,6 +876,12 @@ function $CompileProvider($provide) {
873876
compile: function(element, attr) {
874877
if (interpolateFn) {
875878
return function(scope, element, attr) {
879+
if (name === 'class') {
880+
// we need to interpolate classes again, in the case the element was replaced
881+
// and therefore the two class attrs got merged - we want to interpolate the result
882+
interpolateFn = $interpolate(attr[name], true);
883+
}
884+
876885
// we define observers array only for interpolated attrs
877886
// and ignore observers for non interpolated attrs to save some memory
878887
attr.$observers[name] = [];

test/service/compilerSpec.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ describe('$compile', function() {
391391
}));
392392

393393

394-
it('should merge attributes', inject(function($compile, $rootScope) {
394+
it('should merge attributes including style attr', inject(function($compile, $rootScope) {
395395
element = $compile(
396396
'<div><div replace class="medium-log" style="height: 20px" ></div><div>')
397397
($rootScope);
@@ -431,6 +431,39 @@ describe('$compile', function() {
431431
$rootScope.$digest();
432432
expect(element.text()).toEqual('Hello: 1; Hello: 2; ');
433433
}));
434+
435+
436+
it('should merge interpolated css class', inject(function($compile, $rootScope) {
437+
element = $compile('<div class="one {{cls}} three" replace></div>')($rootScope);
438+
439+
$rootScope.$apply(function() {
440+
$rootScope.cls = 'two';
441+
});
442+
443+
expect(element).toHaveClass('one');
444+
expect(element).toHaveClass('two'); // interpolated
445+
expect(element).toHaveClass('three');
446+
expect(element).toHaveClass('log'); // merged from replace directive template
447+
}));
448+
449+
450+
it('should merge interpolated css class with ng-repeat',
451+
inject(function($compile, $rootScope) {
452+
element = $compile(
453+
'<div>' +
454+
'<div ng-repeat="i in [1]" class="one {{cls}} three" replace></div>' +
455+
'</div>')($rootScope);
456+
457+
$rootScope.$apply(function() {
458+
$rootScope.cls = 'two';
459+
});
460+
461+
var child = element.find('div').eq(0);
462+
expect(child).toHaveClass('one');
463+
expect(child).toHaveClass('two'); // interpolated
464+
expect(child).toHaveClass('three');
465+
expect(child).toHaveClass('log'); // merged from replace directive template
466+
}));
434467
});
435468

436469

0 commit comments

Comments
 (0)