【发布时间】:2017-03-19 21:44:22
【问题描述】:
我正在尝试创建一个可重用的 angular2 组件,该组件接受我服务器上 html 文件的 URL 数组,并创建一个带有选项卡的内容窗口,以在“章节”之间切换,有效地交换内容窗口内的 html 和 css。我尝试了各种各样的东西,包括 iframe,但那些都不起作用,我可以在 StackOverflow 上找到的 angular 1 ng-include 变通办法,但它们都已被弃用,而我最接近的是构建一个组件您可以@Input html 并插入内容,但样式不会应用,并且角度会删除任何样式或脚本标签。这是我尝试过的。
在我的父组件类中:
htmlInput: string = "<h1>Why Does Angular make this so hard?</h1>";
cssInput: string = "h1 { color:red; }"
父组件 HTML:
<app-html [html]='htmlInput' [css]='cssInput'></app-html>
我的 HTML 组件:
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-html',
template: '<div [innerHtml]=html></div>', //This works but no style
//template: '{{html}}', //This displays the actual markup on page
styles: ['{{css}}'] //This does nothing
//styles: ['h1 { color: red; }']//Also nothing
})
export class HtmlComponent implements OnInit {
@Input() html: string = "";
@Input() css: string = "";
ngOnInit() {
}
}
这段代码的结果是
为什么 Angular 让这件事变得如此困难?
但没有红色。也许在将 innerHtml 添加到 DOM 之前应用样式?我不知道,但只是放置 {{html}} 结果会显示带有可见 h1 标签的实际标记。
我想这样做的原因是,在我对网站进行角度化之前,我已经在服务器上的一个文件夹中创建了一堆 HTML 页面,它们都共享一个样式表。我希望能够像书中的页面一样翻阅它们,而无需重新加载页面,并且由于页面太多而且我可能会一直添加更多,我真的不想为每个页面创建路由一。 (我已经有基本站点导航的路由。)
关于如何在最新版本的 Angular 2 中将样式化的 HTML 动态嵌入到页面中,是否有人有更好的建议?在发布这篇文章时,我们处于 2.0.0-beta.17 中。
或者...我已经认为我可能从完全错误的角度来处理这个问题。 Angular 让这件事变得如此困难并且不赞成人们提出的所有解决方案肯定是有原因的,所以如果有人对我如何以更友好的角度实现相同的结果提出建议,我也很乐意听到。
谢谢。
编辑:
我能够通过创建一个管道来解决我的问题,该管道在将 html 添加到 iframe 之前对其进行清理。
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(url: string) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
然后你可以将你的 html 传递到 iframe 中。
<iframe width="100%" height="1000" frameBorder="0" [src]="url | safe"></iframe>
这对我很有用,因为我有一些使用各种 jquery 和样式等的旧页面。这是让它们显示出来的快速修复。
【问题讨论】:
标签: html css angular typescript angular2-template