Html 角度5@输入宽度并应用于div

Html 角度5@输入宽度并应用于div,html,angular,input,Html,Angular,Input,这必须很容易。尝试输入一个字符串,比如50%,50px,20rem。。。作为宽度,然后将其应用于组件的html <test title="frump" width="50%"></test> export class test { @Input() title: string; @Input() width: string; ... getWidth() { return this.width; } <div [ngStyl

这必须很容易。尝试输入一个字符串,比如50%,50px,20rem。。。作为宽度,然后将其应用于组件的html

<test title="frump" width="50%"></test>

export class test {
  @Input() title: string;
  @Input() width: string;
       ...
  getWidth() {
    return this.width;
  }

<div [ngStyle]="{'width.px': 'getWidth()', ... or
<div [ngStyle]="{'width': 'width', ...

导出类测试{
@输入()标题:字符串;
@Input()宽度:字符串;
...
getWidth(){
返回这个.width;
}

看起来您的问题是围绕“宽度”(第二个)的引号。它是一个变量,因此不应包含在引号中。我的示例: