[Fixed] How to bind a property to style.width in pixels?


I’m using Angular 2 and I’ve written the below code:

<div [style.width.px]="width">some texts </div>

I’ve also tried:

<div [ngStyle]="{'width.px': width}">some texts </div>

export class MyComponent
 width: number = 150;

But it doesn’t bind the width to the div element.

What am I doing wrong?


Works fine for me

Plunker example

  selector: 'my-app',
  styles: [`div { border: 3px solid red; }`]
  template: `
      <h2>Hello {{name}}</h2>
      <div [style.width.px]="width">some texts </div>

export class App {
  width: number = 250;
  constructor() {
    this.name = 'Angular2'

Leave a Reply

(*) Required, Your email will not be published