[英]Open an angular template reference in a new browser window
So here is the explanation of the problem I am facing.所以这里是我面临的问题的解释。 It might look very similar other already asked questions, but none of them answered my problem.它可能看起来与其他已经提出的问题非常相似,但没有一个回答我的问题。
I want to open an angular template reference in a new browser window (with all the styles) and use that window to print the contents using system print dialog.我想在新的浏览器 window (包含所有样式)中打开一个 angular 模板引用并使用该 window 使用系统打印对话框打印内容。
By template reference I mean a whole component or may be just a fraction of template of given component.模板引用是指整个组件,或者可能只是给定组件模板的一部分。
Please note that I do not want to do it by below methods:请注意,我不想通过以下方法做到这一点:
Ok, This is how I did it using ComponentFactoryResolver
and Injector
.好的,这就是我使用ComponentFactoryResolver
和Injector
的方式。 Just inject these two dependencies in your component which wants to open other component ( ReportComponent
in this case) in new browser window.只需在想要在新浏览器ReportComponent
中打开其他组件(本例中为 ReportComponent)的组件中注入这两个依赖项。
The constructor looks something like this in snippet below.构造函数在下面的代码片段中看起来像这样。
constructor(
private resolver: ComponentFactoryResolver,
private injector: Injector
) {}
. . . . . .
This method opens up the component in new browser window.此方法在新浏览器 window 中打开组件。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.