some...">

How to bind a property to a Pixel Width style in Angular 2?

I am using Angular 2.0 and I wrote the code below:

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

I also tried

 <div [ngStyle]="{'width.px': width}">some texts </div> export class MyComponent { width: number = 150; } 

But it does not bind the width to the div element.

I am using the latest version of Angular 2.0.

What am I doing wrong?

+29
source share
4 answers

Works great for me

Plunger 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' } } 
+37
source

This worked for me:

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

Check the same as below:

 <div [style.width]="width+'px'">some texts </div> export class MyComponent { width: number = 150; } 
+2
source

For pixel

 <div [style.width.px]="width"> Width Size with px</div> 

Or

 <div bind-style.width.px="width"> Width Size with px</div> 

Other CSS Modules

 <div [style.width.em]="width"> Width Size with em</div> <div [style.width.pt]="width"> Width Size with pt</div> <div [style.width.%]="width"> Width Size with %</div> 

Component

 export class MyComponent { width: number = 80; } 
0
source

Source: https://habr.com/ru/post/1012834/


All Articles