[英]Binding value to <Style> tag Angular 2 template
我有一个使用html文件模板的组件,这样的模板
<div class="abc"></div> <style> .abc { background-color: {{myColor}} } </style>
无论如何动态绑定背景颜色的值? 实际上我可以通过将css移动到内联html这样做<div class="abc" [style.background-color]="myColor"></div>
,但由于某种原因我无法做到这一点。
有没有人有任何想法? 非常感谢你!
您可以使用ng2样式或编写自己的装饰器,根据ng2样式代码插入作为文本导入的css
在四个装饰器功能,您可以使用:
metaInformation = window['Reflect'].getOwnMetadata('annotations', target)
接着:
for (let metadata of metainformation){
if (metadata instanceof ComponentMetadata){
//interpolate somecsstext and add it to to metadata.styles
//as if it was added from the styles property
//of the component decorator metadata
metadata.styles = [...metadata.styles, somecsstext
.replace(/{{([a-z1-9]+)}}/ig, function (match, prop) {
return yourVaribles[prop];
})];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.