X Tutup
Skip to content

Commit f1989e7

Browse files
fix(compiler): remove style when [style.foo]='exp' evaluates to null
Fixes #5110 Closes #5114
1 parent a69e7fe commit f1989e7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

modules/angular2/src/core/linker/view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ export class AppView implements ChangeDispatcher, RenderEventDispatcher {
162162
this.renderer.setElementClass(elementRef, b.name, currentValue);
163163
} else if (b.isElementStyle()) {
164164
var unit = isPresent(b.unit) ? b.unit : '';
165-
this.renderer.setElementStyle(elementRef, b.name, `${currentValue}${unit}`);
165+
this.renderer.setElementStyle(elementRef, b.name,
166+
isPresent(currentValue) ? `${currentValue}${unit}` : null);
166167
} else {
167168
throw new BaseException('Unsupported directive record');
168169
}

modules/angular2/test/core/linker/integration_spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ export function main() {
193193
});
194194
}));
195195

196+
it('should remove style when when style expression evaluates to null',
197+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
198+
tcb.overrideView(MyComp,
199+
new ViewMetadata({template: '<div [style.height.px]="ctxProp"></div>'}))
200+
201+
.createAsync(MyComp)
202+
.then((fixture) => {
203+
204+
fixture.debugElement.componentInstance.ctxProp = '10';
205+
fixture.detectChanges();
206+
expect(DOM.getStyle(fixture.debugElement.componentViewChildren[0].nativeElement,
207+
'height'))
208+
.toEqual('10px');
209+
210+
fixture.debugElement.componentInstance.ctxProp = null;
211+
fixture.detectChanges();
212+
expect(DOM.getStyle(fixture.debugElement.componentViewChildren[0].nativeElement,
213+
'height'))
214+
.toEqual('');
215+
216+
async.done();
217+
});
218+
}));
219+
196220
it('should consume binding to property names where attr name and property name do not match',
197221
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
198222
tcb.overrideView(MyComp,

0 commit comments

Comments
 (0)
X Tutup