angular append style to component

时间:2019-04-08 13:08:13

标签: angular

by default angular appends the component style to the <header> tag of the page.

is there any way to modify that so the component's style will append to the component?

what I am trying to do is creating component and send it to printer.

currently I am using ComponentFactoryResolver to create the component's instance and appending the style manually:

const componentRef = this.resolver.resolveComponentFactory(MyComponent)
    .create(this.injector);
    appRef.attachView(componentRef.hostView);
    const domElem = (componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
    this.document.body.appendChild(domElem);
    domElem.insertAdjacentHTML('afterbegin', '<style>' + PrinterStyle + '</style>');

What I want is that angular compiler will generate the style inside the component and won't add it to the header.

0 个答案:

没有答案