【发布时间】:2018-03-26 09:46:49
【问题描述】:
我想知道是否可以通过 Angular 的 @component 对象的 'styles' 属性来设置组件的样式。
这是我想要实现的示例,请注意“样式”属性中的 {{ratio}} 变量。
@Component({
selector: 'img-thumb',
templateUrl: './img-thumb.component.html',
styleUrls: ['./img-thumb.component.css'],
styles: [`
:host:after{
content: '';
display: block;
padding-bottom: {{ratio}};
}
`],
host: {
'[style.height.px]' : 'height',
'[style.padding.px]' : 'gap'
}
})
export class ImgThumbComponent implements OnInit {
@Input() height : any
@Input() gap : any
@Input() ratio : any
//More code...
}
基本上,我正在尝试使用变量在主机上实现 :after 。 如果可能的话,我更喜欢在组件的 .ts 文件中实现它。
【问题讨论】:
-
插值语法仅适用于模板,不适用于样式。
-
@jonrsharpe 感谢您的快速回答。我使用插值语法来演示我想要实现的目标。
-
您可以访问
img-thumb.component.html文件吗?如果您愿意修改该文件,我将使用[ngStyle]属性投标语法直接在HTML 元素上使用@input 变量的值。 -
@Treeindev 是的,我可以随意修改 html 文件。虽然我不确定如何使用 [ngStyle] 在主机组件上添加 :after?
-
如果您想从班级访问主机,请参见例如stackoverflow.com/a/34643330/3001761
标签: angular typescript