码迷,mamicode.com
首页 > 编程语言 > 详细

[Angular] Modify :before / :after value from Javascirpt

时间:2020-03-31 09:15:16      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:asc   some   import   from   ppc   use case   css   use   cannot   

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

标签:asc   some   import   from   ppc   use case   css   use   cannot   

原文地址:https://www.cnblogs.com/Answer1215/p/12602678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!