【发布时间】:2020-05-30 08:28:45
【问题描述】:
我无法向 Angular 8 显示 html 内容。
**注意:我正在接收我需要在 Angular 上显示的 html 模板作为 JSON 来自数据库,我正在获取它并将其保存到一个变量(例如,绘图),然后将该变量传递给 @ 987654321@
出于演示目的,我放置了一个与我从数据库中获取的数据类似的示例 html 模板。
HTML:
<div [innerHTML]="myTemplate"></div>
app.component.ts:
import { Component, OnInit} from '@angular/core';
import { DomSanitizer, SafeHtml} from '@angular/platform-browser';
@Component({
selector: 'app-data-decomposition',
templateUrl: './data-decomposition.component.html',
styleUrls: ['./data-decomposition.component.css']
})
export class DataDecompositionComponent implements OnInit {
myTemplate:SafeHtml;
constructor(private sanitizer: DomSanitizer) { }
this.myTemplate = this.sanitizer.bypassSecurityTrustHtml(`<html>
<head>
<!-- Load plotly.js into the DOM -->
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
</head>
<body>
<p>plot come below...</>
<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
<script type="text/javascript">
var data = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('myDiv', data);
</script>
</body>
</html>`);
}
当我运行代码时,只有 <p> 标签内容“Plot come below..”显示到 Angular 前端,但 <script> 标签内的 Plotly 图表没有显示。谁能建议我哪里出错了。
【问题讨论】:
标签: html css typescript plot angular8