[英]Angular: Rendering and Getting the HTML of a Component Dynamically
渲染组件的 HTML 以便它可以用于打开类型 about:blank 的新选项卡(与应用程序本身无关的空白新 html 页面)。
为什么? 为了避免使用字符串变量创建 HTML,例如:
var html = '<div>
<h3>My Template</h3>
' + myProperty + '
</div>';
我看到您可以使用ViewContainerRef.createComponent
和ComponentFactoryResolver
动态呈现组件。 这样做的问题是组件在应用程序中的视图容器内呈现。 我想做的是生成该组件的 HTML 以便我可以使用该 HTML 将它放在我想要的任何地方。 (在这种情况下,在window.open()
方法给出的 object 的文档属性中)
例子:
@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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.