Skip to content

Commit bedf537

Browse files
imirkindylhunn
authored andcommitted
fix(common): allow null/undefined to be passed to ngStyle input (#47069)
This brings the typing of ngStyle into parity with ngClass since commit e2ab99b. Should help with some strict template checking edge cases. PR Close #47069
1 parent 5e15e4f commit bedf537

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

goldens/public-api/common/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ export class NgStyle implements DoCheck {
549549
// (undocumented)
550550
set ngStyle(values: {
551551
[klass: string]: any;
552-
} | null);
552+
} | null | undefined);
553553
// (undocumented)
554554
static ɵdir: i0.ɵɵDirectiveDeclaration<NgStyle, "[ngStyle]", never, { "ngStyle": "ngStyle"; }, {}, never, never, true>;
555555
// (undocumented)

packages/common/src/directives/ng_style.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ import {Directive, DoCheck, ElementRef, Input, KeyValueChanges, KeyValueDiffer,
4949
standalone: true,
5050
})
5151
export class NgStyle implements DoCheck {
52-
private _ngStyle: {[key: string]: string}|null = null;
52+
private _ngStyle: {[key: string]: string}|null|undefined = null;
5353
private _differ: KeyValueDiffer<string, string|number>|null = null;
5454

5555
constructor(
5656
private _ngEl: ElementRef, private _differs: KeyValueDiffers, private _renderer: Renderer2) {}
5757

5858
@Input('ngStyle')
59-
set ngStyle(values: {[klass: string]: any}|null) {
59+
set ngStyle(values: {[klass: string]: any}|null|undefined) {
6060
this._ngStyle = values;
6161
if (!this._differ && values) {
6262
this._differ = this._differs.find(values).create();

packages/common/test/directives/ng_style_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
5555
expectNativeEl(fixture).toHaveCssStyle({'max-width': '30%'});
5656
}));
5757

58+
it('should remove styles with a null expression', waitForAsync(() => {
59+
const template = `<div [ngStyle]="expr"></div>`;
60+
fixture = createTestComponent(template);
61+
62+
getComponent().expr = {'max-width': '40px'};
63+
fixture.detectChanges();
64+
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
65+
66+
getComponent().expr = null;
67+
fixture.detectChanges();
68+
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
69+
}));
70+
71+
it('should remove styles with an undefined expression', waitForAsync(() => {
72+
const template = `<div [ngStyle]="expr"></div>`;
73+
fixture = createTestComponent(template);
74+
75+
getComponent().expr = {'max-width': '40px'};
76+
fixture.detectChanges();
77+
expectNativeEl(fixture).toHaveCssStyle({'max-width': '40px'});
78+
79+
getComponent().expr = undefined;
80+
fixture.detectChanges();
81+
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
82+
}));
83+
5884
it('should add and remove styles specified using style.unit notation', waitForAsync(() => {
5985
const template = `<div [ngStyle]="{'max-width.px': expr}"></div>`;
6086

0 commit comments

Comments
 (0)