[Angular] Modify :before / :after value from Javascirpt
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Angular] Modify :before / :after value from Javascirpt相关的知识,希望对你有一定的参考价值。
Let‘s say we want to dynamiclly change some style in :before or :after element.
We cannot use NgStyle, it doesn‘s support this use case, what we can do:
Using css variable + setProperty
import { Component, ElementRef, ViewChild } from ‘@angular/core‘; @Component({ selector: ‘my-app‘, template: ` <p #ref> Start editing to see some magic happen :) </p> `, styles: [ ` :host { --color: blue; } p { font-family: Lato; color: green } p:before { content: ‘content from :before‘; color: var(--color); } ` ] }) export class AppComponent { @ViewChild(‘ref‘) p: ElementRef ngOnInit() { this.p.nativeElement.style.setProperty(‘--color‘, ‘red‘) } }
以上是关于[Angular] Modify :before / :after value from Javascirpt的主要内容,如果未能解决你的问题,请参考以下文章