Lak*_*ngh 11 html javascript css angular angular5
我正在开发angular 5应用程序,我需要在模板中的样式标记中应用动态css.我尝试了一些解决方案,但它们没有用.
app.component.ts
customCss : any;
constructor(){}
ngOnInit(){
this.customCss['color'] = "red";
}
Run Code Online (Sandbox Code Playgroud)
app.component.html
<div>
<span class="custom_css">This is angular 5 application</span>
</div>
<style>
.custom_css{
color: {{customCss.color}};
}
</style>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中检查custom_css类时,它显示的样式
.custom_css{
color: {{customCss.color}};
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
Hum*_*erd 10
顺便说一句,如果你像这样设置颜色:
<div [style.color]="color"></div>
Run Code Online (Sandbox Code Playgroud)
哪里color='var(--cssValue)'行不通!
但是,这可以正常工作:
<div [ngStyle]="{color: color}"></div>
Run Code Online (Sandbox Code Playgroud)
您可以使用[ngStyle]指令:
<span [ngStyle]="{'color': 'red'}">
This is angular 5 application
</span>
Run Code Online (Sandbox Code Playgroud)
或者像这样:
<span [ngStyle]="applyStyles()">
This is angular 5 application
</span>
Run Code Online (Sandbox Code Playgroud)
在组件中:
applyStyles() {
const styles = {'color' : 'red'};
return styles;
}
Run Code Online (Sandbox Code Playgroud)
如果您在给定的组件中几乎没有要更改的元素,则给定的答案有效,如果您需要根据用户的选择(并且即时)更改应用程序的完整整体外观和感觉,则到目前为止我发现的唯一方法是在 javascript 中覆盖 css,如下所示:
this.stylesService.get().subscribe(({ customStyles }) => {
const style = document.createElement('style');
style.innerHTML =
`.picture {
background-image: url(${customStyles.backgroundUrl});
}
h1, h2 {
color: ${customStyles.primaryColor};
}`;
document.body.appendChild(style);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6608 次 |
| 最近记录: |