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

Issue

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?

Solution

Works fine for me

Plunker example

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

    </div>
  `,
})
export class App {
  name:string;
  width: number = 250;
  constructor() {
    this.name = 'Angular2'
  }
}

Leave a Reply

(*) Required, Your email will not be published