Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 47248b1

Browse files
CaerusKaruThomasBurleson
authored andcommitted
fix(ngClass): should properly remove classes without fallback (#995)
In some cases where a fallback `ngClass` is not defined, the ClassDirective had no way of knowing to remove its ngClass on deactivation. > Unlike other directives, the `removeStyles` action has no effect on ClassDirective. By specifying an empty default fallback, we ensure that on deactivation - if no other default is specified - the ClassDirective will correctly fallback to an empty string case. Fixes #992.
1 parent 8307655 commit 47248b1

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/lib/extended/class/class.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ describe('class directive', () => {
8787
expectNativeEl(fixture).not.toHaveCssClass('class2');
8888
});
8989

90+
it('should use responsive ngClass string and remove without fallback', () => {
91+
createTestComponent(`<div [ngClass.xs]="'what class2'"></div>`);
92+
93+
expectNativeEl(fixture).not.toHaveCssClass('what');
94+
expectNativeEl(fixture).not.toHaveCssClass('class2');
95+
96+
// the CSS classes listed in the string (space delimited) are added,
97+
// See https://angular.io/api/common/NgClass
98+
mediaController.activate('xs');
99+
expectNativeEl(fixture).toHaveCssClass('what');
100+
expectNativeEl(fixture).toHaveCssClass('class2');
101+
102+
mediaController.activate('lg');
103+
expectNativeEl(fixture).not.toHaveCssClass('what');
104+
expectNativeEl(fixture).not.toHaveCssClass('class2');
105+
});
106+
90107
it('should override base `class` values with responsive ngClass map', () => {
91108
createTestComponent(`
92109
<div class="class0" [ngClass.xs]="{'what':true, 'class2':true, 'class0':false}"></div>

src/lib/extended/class/class.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class ClassDirective extends BaseDirective2 implements DoCheck {
4949
);
5050
}
5151
this.init();
52+
this.setValue('', '');
5253
}
5354

5455
protected updateWithValue(value: any) {

0 commit comments

Comments
 (0)