[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‘)
  }
}

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

时间: 2024-10-11 23:13:29

[Angular] Modify :before / :after value from Javascirpt的相关文章

半小时入门Angular 2

本文首发于由电子工业出版社出版<揭秘Angular 2>一书,基于第5章概览改写. 作者简介:广发证券互联网金融技术团队,是Angular早期坚定的践行者.作为全新一代的证券业 IT 研发组织,团队致力于用更新更好的技术打造行业方案.支持业务创新. 责编:陈秋歌,寻求报道或者投稿请发邮件至chenqg#csdn.net,或加微信:Rachel_qg. 了解更多前沿技术资讯,获取深度技术文章推荐,请关注CSDN研发频道微博. Angular 2.0 于去年 9 月正式发布. 尽管同一时间里 Re

[Angular] @ViewChildren and QueryLists (ngAfterViewInit)

When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This is somehow different from @ViewChild, which value can be accessed from ngAfterContentInit lifecycle. import { Component, ChangeDetectorRef, Output, Vi

[AngularJS] Using Services in Angular Directives

Directives have dependencies too, and you can use dependency injection to provide services for your directives to use. Bad: If you want to use <alert> in another controller or page, you have to modify the AlertService. This might break things. <!

angular表单验证实例----可用的代码

前段时间,公司做一个单页面,就是一个表单验证,早开始在菜鸟教程上关注了angular,所以下派上用场了 angular里面对于表单验证,设置了很多指令. 也就是说不用自己写一些逻辑,直接绑定指令就行. ng-app     启动你angular的模块 ng-controller 控制器,启动你angualr里面的逻辑代码作用在页面上 ng-options  循环你select里面的option标签,很好用的 ng-submit,表单提交执行的 novalidate  表单form配合后期检测的

[Angular 2] Directive intro and exportAs

First, What is directive, what is the difference between component and directive. For my understanding, component is something like 'canvas', 'form', 'table'... they have the template and their own functionality. It defines how a html tag should work

Access-Control-Allow-Origin: Dealing with CORS Errors in Angular

https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/ Getting this error in your Angular app? No ‘Access-Control-Allow-Origin’ header is present on the requested resource. You’ve run afoul of the Same Origin Policy – it says that

[Angular Directive] 3. Handle Events with Angular 2 Directives

A @Directive can also listen to events on their host element using @HostListener. This allows you to add behaviors that react to user input and update or modify properties on the element without having to create a custom component. import {Directive,

Angular DirtyChecking(脏值检查) $watch, $apply, $digest

Dirty Checking (脏值检查) Digest cycle and $scope Digest cycle and $scope First and foremost, AngularJS defines a concept of a so-called digest cycle. This cycle can be considered as a loop, during which AngularJS checks if there are any changes to all t

Angular 2 + 折腾记 :(7) 初步了解表单:模板驱动及数据驱动及脱坑要点

前言 表单在整个系统中的作用相当重要,这里主要扯下响应表单的实现方式. 首先需要操作表单的模块引入这两个模块: import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 表单控件响应的几种状态 模板驱动表单依赖FormsModule,数据驱动的表单依赖FormsModule,ReactiveFormsModule 一般做表单校验及操作推荐用数据驱动的方式,好维护和理解.. 模板驱动 模板驱动:主要是依赖[(ngModel