📜  如何在 Angular 中使用 nativeelement 添加 css - CSS 代码示例

📅  最后修改于: 2022-03-11 14:47:31.183000             🧑  作者: Mango

代码示例1
import { ..., AfterViewInit, ElementRef, ViewChild, Renderer2, ... } from '@angular/core';
...
export class AdvertisementDirective implements AfterViewInit {

  @ViewChild('templateRefName') el: ElementRef;

  constructor(
    private renderer: Renderer2
  ) {
  }

  ngAfterViewInit() {
    this.renderer.setStyle(this.el.nativeElement, 'background', 'yellow');
    this.renderer.setProperty(this.el.nativeElement, 'innerHTML', '

Hello World

'); } }