Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a property to style.width in pixels?

Tags:

angular

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?

like image 340
Dynamic Avatar asked Dec 02 '16 10:12

Dynamic


4 Answers

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'
  }
}
like image 61
Günter Zöchbauer Avatar answered Nov 06 '22 15:11

Günter Zöchbauer


This worked for me:

<div [style.width]="paymentPerc+'%'"> Payment Recieved</div>
like image 40
Bokang Masilo Avatar answered Nov 06 '22 15:11

Bokang Masilo


For pixel unit

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

Or

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

Other CSS units

<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;
}
like image 19
ElasticCode Avatar answered Nov 06 '22 14:11

ElasticCode


Check with same as below once:

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

export class MyComponent
{
 width: number = 150;
}
like image 7
ranakrunal9 Avatar answered Nov 06 '22 14:11

ranakrunal9