微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角度样式不适用于renderer2创建的元素

如何解决角度样式不适用于renderer2创建的元素

内部应用程序组件

import { Component,ViewChild,Renderer2,ElementRef } from '@angular/core';


@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
//I'm using renderer2 inside component not directory

export class AppComponent {
  @ViewChild('div1') el : ElementRef;
  showComment = false;
  constructor(private renderer: Renderer2){}
  ngOnInit() {}

  ngAfterViewInit() {
    const div = this.renderer.createElement('div');
    div.innerHTML=`<p id='comment' #comment1 *ngIf='showComment'>Test<p>`;
    this.renderer.appendChild(this.el.nativeElement,div);
  }
}

样式不适用于在app.component.css中使用renderer2 创建的'div'和'p'标签

我什至看不到使用viewchild,并且ngif也不起作用,这不属于应用程序组件。

除了在全局添加样式外,如何在同一组件中应用样式**,我该如何使*ngif工作。Angular的新手请帮忙。

解决方法

似乎是ViewEncapsulation问题。 ViewEncapsulation。没有人可以解决它:

@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css'],encapsulation: ViewEncapsulation.None
})
export class AppComponent {
  ...
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。