Set component style from variable in Angular 2

后端 未结 3 829
野性不改
野性不改 2020-12-17 15:21

My goal is to set a style (height and width in this case) from a component variable using the \"styles\" attribute. I would think there is a simple data binding method but t

3条回答
  •  鱼传尺愫
    2020-12-17 16:03

    You can style the host element like

    @Component({
      selector: '[sidebar]',
      templateUrl: 'app/Nav/sidebar.comp.html',
      host: {
        '[style.height.px]':'0.9 * height',
        '[style.width.px]':'0.21 * width'
      }
    
    })
    
    export class SidebarComp {
        width: number;
        height: number;
    
        constructor() {
            this.height = window.innerHeight;
            this.width = window.innerWidth;
        }
    }
    

    and the content (app/Nav/sidebar.comp.html) like

    or

提交回复
热议问题