【发布时间】:2020-01-31 23:45:04
【问题描述】:
渲染组件的 HTML,以便它可以用于打开 about:blank 类型的新选项卡(与应用程序本身无关的空白新 html 页面)。
为什么? 为了避免使用字符串变量创建 HTML,例如:
var html = '<div>
<h3>My Template</h3>
' + myProperty + '
</div>';
我看到您可以使用ViewContainerRef.createComponent 和ComponentFactoryResolver 动态呈现组件。这样做的问题是组件在应用程序中的视图容器内呈现。我想做的是生成该组件的 HTML,这样我就可以使用该 HTML 将它放在我想要的任何地方。 (在这种情况下,在window.open() 方法给出的对象的文档属性中)
示例:
@Component({
selector: 'app-component',
template: `
<div>
<h3>My Template</h3>
{{ myProperty }}
</div>
`,
styleUrls: ['./my.component.less']
})
export class MyComponent{
myProperty: string;
}
我希望以这种方式使用它:
//get new window tab
var newTab = window.open('about:blank', '_blank');
//get the HTML of the component
//use it to open the new tab
newTab.document.write(html);
【问题讨论】:
-
@ChristopherPeisert 我正在那里查看答案。明天我会告诉你这些是否对我有用。谢谢!
标签: html angular typescript components angular8