ngStyle:说"Missing expected }"时出错



在解释时,我从chrome控制台收到以下错误

<div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat': 'no-repeat'; 'border-radius': '0px';}"></div>

未捕获错误:模板分析错误:分析器错误:应为缺少}在[{'width':"100%";'height':"100%";'背景大小':'封面';'background repeat':'无重复';"边界半径":"0px";}]在ng:///AppModule中/HomeComponent.html@31:29("="宽度:100%;高度:100%;">

应该是什么原因导致错误?

与采用CSS样式的style属性不同,ngStyle采用javascript对象(用字符串表示(。这就是为什么您必须用引号'100%'和其他属性(如background-size(包裹100%,因为在javascript属性名称/值中%-字符是非法的。

CSS

blah {
attribute: value;
attribute-dash: value;
}

对象

{
attribute: value,
'attribute-dash': value
}

因此,您需要将;替换为,

<div [ngStyle]="{'width': '100%', 'height': '100%', 'background-size': 'cover', 'background-repeat': 'no-repeat', 'border-radius': '0px'}"></div>

注意:如果要设置动态值,则应该使用ngStyle。它允许您将变量传递到样式中,并使用布尔值切换特定样式。如果您只是想设置内联样式,那么应该使用普通的style属性。

在新版本中,您也可以使用unit。

键是一个样式名称,带有可选项。后缀(如"top.px"、"font-style.em"(.

类似

[ngStyle]="{'width.px':200, 'height.px':200}"

相关内容

最新更新