如何将属性绑定到Angular 2中的Style Width Pixel?

问题描述:

我使用的是2.0角,我已经写了下面的代码:如何将属性绑定到Angular 2中的Style Width Pixel?

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

我也试过

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

export class MyComponent 
{ 
width: number = 150; 
} 

但它不结合宽度的div元素。

我使用的是Angular 2.0的最新版本。

我在做什么错?

+0

你为什么不在n的结尾连接'px'赭? –

工作正常,我

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' 
    } 
} 

请与以下同一次:

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

export class MyComponent 
{ 
width: number = 150; 
} 

这为我工作:

<div [style.width]="paymentPerc+'%'"> Payment Recieved</div>