简体   繁体   English

HTML代码未呈现,仅显示为纯文本

[英]HTML code is not rendering, just being displayed as plaintext

I have some HTML, but for some reason it's showing up as plaintext and it's not rendering. 我有一些HTML,但是由于某种原因,它显示为纯文本,并且没有呈现。

This is what it looks like: 看起来是这样的:

在此处输入图片说明

The HTML for this is like so: HTML如下所示:

<div>
  <mat-card>
    <button mat-raised-button>Copy Code to Clipboard</button>
    <mat-card-title>Card with title and footer</mat-card-title>
    <mat-card-subtitle>Subtitle</mat-card-subtitle>
    <mat-card-content>
      <code>{{muchoCodo}}</code>
    </mat-card-content>
    <mat-card-actions>
      <button mat-button>LIKE</button>
      <button mat-button>SHARE</button>
    </mat-card-actions>
    <mat-card-footer>
      Card Footer
    </mat-card-footer>
  </mat-card>

</div>

{{muchoCodo}}

The string data is being rendered as the muchoCodo variable. 字符串数据被呈现为muchoCodo变量。 Here is my Angular code: 这是我的Angular代码:

import {Component, OnInit} from '@angular/core';
declare var Prism;

import 'prismjs';
import 'prismjs/themes/prism-okaidia.css'
import 'prismjs/components/prism-handlebars.min.js'
import 'prismjs/components/prism-lua.min.js'

@Component({
  selector: 'app-generated-code',
  templateUrl: './generated-code.component.html',
  styleUrls: ['./generated-code.component.scss'],
  styles: [`
    mat-card { margin:2em; }
  `]
})
export class GeneratedCodeComponent implements OnInit {

  muchoCodo: string;

  constructor() {

    this.muchoCodo = Prism.highlight(`

    import suman = require('suman');
    const {Test} = suman.init(module);

    Test.create((b, it, before) => {

      it('bahru', t => {

      });

    });


    `, Prism.languages.javascript);

  }

  ngOnInit() {
  }

}

Anybody know how I can get the string to render as HTML? 有人知道我怎样才能将字符串呈现为HTML吗?

In order to render the text as HTML, you have to bind to the innerHTML property of the desired container element. 为了将文本呈现为HTML,您必须绑定到所需容器元素的innerHTML属性。

<mat-card-content>
  <code [innerHTML]="muchoCodo"></code>
</mat-card-content>

Keep in mind that this is a potential security issue and you should trust the HTML content you are going to render on the page. 请记住,这是一个潜在的安全问题,您应该信任要在页面上呈现的HTML内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM