Currently, I am developing a basic animation library that allows users to customize components using property binding. Up to this point, I have been applying their selections like so:
<div [style.background]="color" [style.width.px]="width" [style.height.px]="height"></div>
However, in order to streamline the process of adding more properties in the future, I want to replace all of this with [ngStyle]="styleObject"
. This is what I've attempted:
@Input() width: number;
@Input() height: number;
public styleObject: Object = {
'height': this.height,
'width': this.width
};
Unfortunately, it seems that
<div [ngStyle]="styleObject"></div>
is not recognizing the styles defined above.
I have tried incorporating + 'px'
and utilizing height.px
, but these methods did not resolve my issue.
What am I overlooking?
--
Through some experimentation, I have confirmed that:
styleObject = {
'height': 200 + 'px',
'background': 'red'
};
functions correctly and is applied to the div
, whereas replacing 200
with this.height
(which is of type number
) does not work as expected.