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

前端 未结 4 1705
时光取名叫无心
时光取名叫无心 2021-02-05 00:10

I\'m using Angular 2 and I\'ve written the below code:

some texts

I\'ve also tried:

相关标签:
4条回答
  • 2021-02-05 00:44

    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'
      }
    }
    
    0 讨论(0)
  • 2021-02-05 00:51

    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;
    }
    
    0 讨论(0)
  • 2021-02-05 00:52

    This worked for me:

    <div [style.width]="paymentPerc+'%'"> Payment Recieved</div>
    
    0 讨论(0)
  • 2021-02-05 00:55

    Check with same as below once:

    <div [style.width]="width+'px'">some texts </div>
    
    export class MyComponent
    {
     width: number = 150;
    }
    
    0 讨论(0)
提交回复
热议问题