Angular: Component scss styles not applied to tags supplied to [innerHTML] of div?

use*_*110 2 javascript css angular-material angular mat-dialog

我有一个 MatDialog,它在构造时由其父级提供了一个 HTML 字符串。HTML 字符串是命名 div 内容的来源,并且包含<table>嵌入 html 中的标签,但由于某种原因,对话框的 scss 文件中定义的表格样式不适用于我指定为 的字符串[innerHTML],尽管它对于<table>标签来说效果很好直接硬编码到html文件中。

有什么方法可以让对话框使用对话框组件的样式模板中包含的样式来渲染innerHTML?

    export class DialogAgreementViewer implements AfterViewInit {
      constructor(public dialogRef:
        MatDialogRef<DialogAgreementViewer>, @Inject
        (MAT_DIALOG_DATA) public data: string, protected _sanitizer : DomSanitizer){
          this.agreementText = this._sanitizer.bypassSecurityTrustHtml(data);
        }
      public agreementText : SafeHtml;
      @ViewChild('agreementText') agreementTextCtl : ElementRef;
    }
Run Code Online (Sandbox Code Playgroud)

HTML:

    <p>Agreement Template</p>
    <table>.          <-- Defined table styles are applied to this table.
        <tbody>
            <tr><td>Title</td><td>Name of Work</td></tr>
        </tbody>
    </table>
    
    <div [innerHTML]='agreementText'></div> <-- Table styles not applied here.
Run Code Online (Sandbox Code Playgroud)

社会保障体系:

    table, td {
        border: 1px solid black;
       
    }
    table {
        border-collapse: collapse;
        width: 75%
    }
Run Code Online (Sandbox Code Playgroud)

Nen*_*vic 5

您应该禁用该组件的封装。你可以这样做:

import { Component, ViewEncapsulation } from '@angular/core';
...
@Component({
  ...
  encapsulation: ViewEncapsulation.None,
})
Run Code Online (Sandbox Code Playgroud)