html - 在新的浏览器窗口中打开角度模板参考
问题描述
所以这里是我面临的问题的解释。它可能看起来与其他已经提出的问题非常相似,但没有一个回答我的问题。
我想在新的浏览器窗口(包含所有样式)中打开一个角度模板引用,并使用该窗口使用系统打印对话框打印内容。
模板引用是指整个组件,或者可能只是给定组件模板的一部分。
请注意,我不想通过以下方法做到这一点:
- 在新的浏览器窗口中打开新路线。(为什么?这将导致所有其他事情,例如公共工具栏或帮助打开组件。还需要一条不希望的新路线。)
- 以可关闭的模式打开内容。
解决方案
好的,这就是我使用ComponentFactoryResolver
and的方式Injector
。只需将这两个依赖项注入您想要ReportComponent
在新浏览器窗口中打开其他组件(在本例中)的组件中。
构造函数在下面的代码片段中看起来像这样。
constructor(
private resolver: ComponentFactoryResolver,
private injector: Injector
) {}
. . .
此方法在新的浏览器窗口中打开组件。
public openPrintableReport(): void {
// resolve and instantiate the component which you want to open in new window.
const componentFactory = this.resolver.resolveComponentFactory(ReportComponent);
let component = componentFactory.create(this.injector);
component.changeDetectorRef.detectChanges();
// define external window
this.externalWindow = window.open('', '', 'width=1000,height=1000,left=150,top=200');
// copy all the styles to external window.
document.querySelectorAll('style').forEach(htmlElement => {
this.externalWindow.document.head.appendChild(htmlElement.cloneNode(true));
});
this.externalWindow.document.title = 'Printer Friendly Report';
// attach the component to external window
this.externalWindow.document.body.appendChild(component.location.nativeElement);
}
推荐阅读
- android - Android图像注释位图问题
- react-native - 如何从 redux 获取 componentDidMount 中的令牌?
- javascript - 数据表在特定索引下方添加行
- python-3.x - 打开时如何让tkinter窗口已经透明?
- javascript - 如何在不同的行中显示超链接
- javascript - 如何在打字稿中对回调的返回类型实现类型保护?
- angular - 如何在 Angular 的 ag-grid 中添加具有行跨度的按钮
- file - 当我使用 google colab 中的 h5py 访问 .hdf 文件时发生错误
- c# - 如何使用 LiveChart Gauge 使 LabelFormatter 只影响一个绑定值
- flutter - 颤动中的iOS搜索栏动画