2016-12-02 23 views
6

Używam kątowa 2,0 i pisałem poniższy kod:Jak powiązać właściwość z stylem szerokości piksela w Angular 2?

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

Próbowałem zostały również

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

export class MyComponent 
{ 
width: number = 150; 
} 

Ale to nie wiąże szerokość do elementu div.

Używam najnowszej wersji Angulara 2.0.

Co robię źle?

+0

dlaczego nie połączyć "px" na końcu n umbra? –

Odpowiedz

11

działa dobrze dla mnie

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

Sprawdź sam, jak z poniżej kiedyś:

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

export class MyComponent 
{ 
width: number = 150; 
} 
1

Ten pracował dla mnie:

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