admin管理员组文章数量:1277540
I am receiving an HTML string from an API response and need to render it inside my Angular component while keeping the component's CSS styles applied.
What I have tried:
Using
[innerHTML]
, but the styles defined in my component's SCSS are not applied:<div class="content" [innerHTML]="htmlContent"></div>
.content h3 { color: red; // This does NOT apply }
Using
DomSanitizer
to bypass security restrictions:import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; htmlContent: SafeHtml; constructor(private sanitizer: DomSanitizer) {} ngOnInit() { this.htmlContent = this.sanitizer.bypassSecurityTrustHtml(apiResponseHtml); }
But the styles from the component still do not apply.
Trying
::ng-deep .content h3 { color: red; }
, but I want to avoid::ng-deep
since it's deprecated.Setting
ViewEncapsulation.None
in my component, but this makes the styles global, which I don’t want.
How can I render the API-provided HTML inside my component while keeping the component's scoped CSS applied?
I am receiving an HTML string from an API response and need to render it inside my Angular component while keeping the component's CSS styles applied.
What I have tried:
Using
[innerHTML]
, but the styles defined in my component's SCSS are not applied:<div class="content" [innerHTML]="htmlContent"></div>
.content h3 { color: red; // This does NOT apply }
Using
DomSanitizer
to bypass security restrictions:import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; htmlContent: SafeHtml; constructor(private sanitizer: DomSanitizer) {} ngOnInit() { this.htmlContent = this.sanitizer.bypassSecurityTrustHtml(apiResponseHtml); }
But the styles from the component still do not apply.
Trying
::ng-deep .content h3 { color: red; }
, but I want to avoid::ng-deep
since it's deprecated.Setting
ViewEncapsulation.None
in my component, but this makes the styles global, which I don’t want.
How can I render the API-provided HTML inside my component while keeping the component's scoped CSS applied?
Share Improve this question edited Feb 25 at 7:40 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Feb 25 at 6:19 panagiotis lymperopoulospanagiotis lymperopoulos 111 silver badge1 bronze badge2 Answers
Reset to default 1You can set ViewEncapsulation.None
and use the :host
selector, so that the styles apply only to this component (host) and it's children:
@Component({
...
selector: 'some-selector'
styles: [`:host .content h3 { color: red; }`],
encapsulation: ViewEncapsulation.None,
...
})
export class SomeComponent {
...
Else just scope the styles based on the component selector in the global-styles.scss.
global-styles.scss
some-selector {
.content h3 {
color: red; // This does NOT apply
}
}
Thanks for your answer.
The third solution is to change the innerHTML from API to have the styles already.
But I need an other solution. ViewEncapsulation and global-styles are not so good solution for the project.
Please share another way if you know, thanks
本文标签: angularHow to apply component CSS to dynamically injected HTML from an APIStack Overflow
版权声明:本文标题:angular - How to apply component CSS to dynamically injected HTML from an API? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741224843a2361663.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论