Skip to content

Commit ee1eebd

Browse files
mnahkieskara
authored andcommitted
fix(common): ngStyle should ignore undefined values (#34422)
Prior to ivy, undefined values passed in an object to the ngStyle directive were ignored. Restore this behavior by ignoring keys that point to undefined values. closes #34310 PR Close #34422
1 parent 05b4f8e commit ee1eebd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/common/src/directives/styling_differ.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ function bulidMapFromValues(
214214
let key = keys[i];
215215
key = trim ? key.trim() : key;
216216
const value = (values as{[key: string]: any})[key];
217-
setMapValues(map, key, value, parseOutUnits, allowSubKeys);
217+
218+
if (value !== undefined) {
219+
setMapValues(map, key, value, parseOutUnits, allowSubKeys);
220+
}
218221
}
219222
} else {
220223
// case 2: array

packages/common/test/directives/ng_style_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ import {ComponentFixture, TestBed, async} from '@angular/core/testing';
157157
expectNativeEl(fixture).not.toHaveCssStyle('max-width');
158158
expectNativeEl(fixture).toHaveCssStyle({'font-size': '12px'});
159159
}));
160+
161+
it('should skip keys that are set to undefined values', async(() => {
162+
const template = `<div [ngStyle]="expr"></div>`;
163+
164+
fixture = createTestComponent(template);
165+
166+
getComponent().expr = {
167+
'border-top-color': undefined,
168+
'border-top-style': undefined,
169+
'border-color': 'red',
170+
'border-style': 'solid',
171+
'border-width': '1rem',
172+
};
173+
174+
fixture.detectChanges();
175+
176+
expectNativeEl(fixture).toHaveCssStyle({
177+
'border-color': 'red',
178+
'border-style': 'solid',
179+
'border-width': '1rem',
180+
});
181+
}));
182+
160183
});
161184
}
162185

0 commit comments

Comments
 (0)