【发布时间】:2018-10-15 10:26:47
【问题描述】:
我创建了一个 Angular 项目,我想在 html 中插入 style CSS,但我不希望插入的 CSS 替换具有相同标签或类名的其他样式。
const testStyle = '<style>
body {color: red}
table {width : 200px}
h1{font-size:12px}
.another-same-class-name{color: blue;font-size: 13px}
</style>'
上面是我想要插入到组件模板中的示例样式
我的组件
@Component({
selector : 'app-my-component',
templateUrl: './my-template.component.html',
styleUrls: ['./my-style.component.scss'],
})
export class MyComponent{
myStyle:string
...
ngOnInit(){
const testStyle = '<style>
body {color: red}
table {width : 200px}
h1{font-size:12px}
.another-same-class-name{color: blue;font-size: 13px}
</style>'
this.myStyle = testStyle
}
updateCss(newCss){
this.myStyle = `<style>${newCss}</style>`
}
}
<div #styleWillGoHere [innerHtml]="myStyle"></div>
编辑:我已经更新了我的问题,让它更清楚:)
我很欣赏任何类型的解决方案。
【问题讨论】:
-
听起来你想要的就是文档中描述的Component Styles。
-
它是否适用于静态样式?你能添加一个stackblitz吗?
标签: javascript css angular