Skip to content

Commit 8b18ef4

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
feat(NgStyle): add support for the style.unit notation (#10496)
Closes #10326
1 parent cd18de7 commit 8b18ef4

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

modules/@angular/common/src/directives/ng_style.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {isBlank, isPresent} from '../facade/lang';
2121
*
2222
* ### Syntax
2323
*
24-
* - `<div [ngStyle]="{'font-style': style}"></div>`
24+
* - `<div [ngStyle]="{'font-style': styleExp}"></div>`
25+
* - `<div [ngStyle]="{'max-width.px': widthExp}"></div>`
2526
* - `<div [ngStyle]="styleExp"></div>` - here the `styleExp` must evaluate to an object
2627
*
2728
* ### Example ([live demo](http://plnkr.co/edit/YamGS6GkUh9GqWNQhCyM?p=preview)):
@@ -93,15 +94,19 @@ export class NgStyle implements DoCheck {
9394
}
9495

9596
private _applyChanges(changes: any): void {
97+
changes.forEachRemovedItem(
98+
(record: KeyValueChangeRecord) => { this._setStyle(record.key, null); });
9699
changes.forEachAddedItem(
97100
(record: KeyValueChangeRecord) => { this._setStyle(record.key, record.currentValue); });
98101
changes.forEachChangedItem(
99102
(record: KeyValueChangeRecord) => { this._setStyle(record.key, record.currentValue); });
100-
changes.forEachRemovedItem(
101-
(record: KeyValueChangeRecord) => { this._setStyle(record.key, null); });
102103
}
103104

104105
private _setStyle(name: string, val: string): void {
105-
this._renderer.setElementStyle(this._ngEl.nativeElement, name, val);
106+
const nameParts = name.split('.');
107+
const nameToSet = nameParts[0];
108+
const valToSet = isPresent(val) && nameParts.length === 2 ? `${val}${nameParts[1]}` : val;
109+
110+
this._renderer.setElementStyle(this._ngEl.nativeElement, nameToSet, valToSet);
106111
}
107112
}

modules/@angular/common/test/directives/ng_style_spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,58 @@ export function main() {
6565
});
6666
}));
6767

68+
it('should add and remove styles specified using style.unit notation',
69+
inject(
70+
[TestComponentBuilder, AsyncTestCompleter],
71+
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
72+
var template = `<div [ngStyle]="{'max-width.px': expr}"></div>`;
73+
74+
tcb.overrideTemplate(TestComponent, template)
75+
.createAsync(TestComponent)
76+
.then((fixture) => {
77+
78+
fixture.debugElement.componentInstance.expr = '40';
79+
fixture.detectChanges();
80+
expect(getDOM().getStyle(
81+
fixture.debugElement.children[0].nativeElement, 'max-width'))
82+
.toEqual('40px');
83+
84+
fixture.debugElement.componentInstance.expr = null;
85+
fixture.detectChanges();
86+
expect(getDOM().getStyle(
87+
fixture.debugElement.children[0].nativeElement, 'max-width'))
88+
.toEqual('');
89+
90+
async.done();
91+
});
92+
}));
93+
94+
it('should update styles using style.unit notation when unit changes',
95+
inject(
96+
[TestComponentBuilder, AsyncTestCompleter],
97+
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
98+
var template = `<div [ngStyle]="expr"></div>`;
99+
100+
tcb.overrideTemplate(TestComponent, template)
101+
.createAsync(TestComponent)
102+
.then((fixture) => {
103+
104+
fixture.debugElement.componentInstance.expr = {'max-width.px': '40'};
105+
fixture.detectChanges();
106+
expect(getDOM().getStyle(
107+
fixture.debugElement.children[0].nativeElement, 'max-width'))
108+
.toEqual('40px');
109+
110+
fixture.debugElement.componentInstance.expr = {'max-width.em': '40'};
111+
fixture.detectChanges();
112+
expect(getDOM().getStyle(
113+
fixture.debugElement.children[0].nativeElement, 'max-width'))
114+
.toEqual('40em');
115+
116+
async.done();
117+
});
118+
}));
119+
68120
// keyValueDiffer is sensitive to key order #9115
69121
it('should change styles specified in an object expression',
70122
inject(

0 commit comments

Comments
 (0)