Dima Lytvyniuk
Dima Lytvyniuk

Reputation: 43

Add component to body with other container component on Angular 6

I have made service which accept component, adding that component to my component and adding them to <body>. Problem is while I have ng-content in my component to place the incoming component, it simply adding it below the ng-content as simply appendChild do.

./service.ts

import {
    Injectable,
    Injector,
    ComponentFactoryResolver,
    EmbeddedViewRef,
    ApplicationRef,
    ComponentRef
} from '@angular/core';
import { ModalComponent } from './modal.component';
@Injectable({
  providedIn: 'root'
})
export class ModalService {
  constructor(
      private componentFactoryResolver: ComponentFactoryResolver,
      private appRef: ApplicationRef,
      private injector: Injector
  ) { }
  appendComponentToBody(component: any) {
    // Create a component reference from the incoming component 
    let componentRef = this.componentFactoryResolver
      .resolveComponentFactory(component)
      .create(this.injector);
    // Attach incoming component to the appRef so that it's inside the ng component tree
    this.appRef.attachView(componentRef.hostView);
    // Get DOM element from incoming component
    let contentElem = (componentRef.hostView as EmbeddedViewRef<any>)
      .rootNodes[0] as HTMLElement;
    // Create a component reference from the service component 
    let componentRefer = this.componentFactoryResolver
      .resolveComponentFactory(ModalComponent)
      .create(this.injector);
    // Attach component to the appRef so that it's inside the ng component tree
    this.appRef.attachView(componentRefer.hostView);
    // Get DOM element from service component
    let domElem = (componentRefer.hostView as EmbeddedViewRef<any>)
      .rootNodes[0] as HTMLElement;
    domElem.appendChild(contentElem);
    // Append DOM element to the body
    document.body.appendChild(domElem);
  }
}

./modal.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'modal',
  template: `
    <div>
      <ng-content>
      </ng-content>
    </div>    
  `,
  styles: []
})
export class ModalComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

result of that service call will be in body if we provide picture component:

<modal>
   <div></div>
   <app-picture _nghost-c2="">
     <p _ngcontent-c2=""> picture works!</p>
   </app-picture>
</modal>

expectation in body:

<modal>
   <div>
     <app-picture _nghost-c2="">
        <p _ngcontent-c2=""> picture works!</p>
     </app-picture>
   </div>
</modal>

Upvotes: 4

Views: 3245

Answers (1)

Honchar Denys
Honchar Denys

Reputation: 1508

As solution you can do javascript solution, by putting incoming DOM into specific tag inside modal DOM. ./service.ts

import {
    Injectable,
    Injector,
    ComponentFactoryResolver,
    EmbeddedViewRef,
    ApplicationRef,
    ComponentRef
} from '@angular/core';
import { ModalComponent } from './modal.component';
@Injectable({
  providedIn: 'root'
})
export class ModalService {
  constructor(
      private componentFactoryResolver: ComponentFactoryResolver,
      private appRef: ApplicationRef,
      private injector: Injector
  ) { }
  appendComponentToBody(component: any) {
    // Create a component reference from the incoming component 
    let componentRef = this.componentFactoryResolver
      .resolveComponentFactory(component)
      .create(this.injector);
    // Attach incoming component to the appRef so that it's inside the ng component tree
    this.appRef.attachView(componentRef.hostView);
    // Get DOM element from incoming component
    let contentElem = (componentRef.hostView as EmbeddedViewRef<any>)
      .rootNodes[0] as HTMLElement;
    // Create a component reference from the service component 
    let componentRefer = this.componentFactoryResolver
      .resolveComponentFactory(ModalComponent)
      .create(this.injector);
    // Attach component to the appRef so that it's inside the ng component tree
    this.appRef.attachView(componentRefer.hostView);
    // Get DOM element from service component
    let domElem = (componentRefer.hostView as EmbeddedViewRef<any>)
      .rootNodes[0] as HTMLElement;
    // Append DOM element to the body
    document.body.appendChild(domElem);
    // Add incoming component to modal component
    domElem.querySelector('#Modal').appendChild(contentElem);
  }
}

./modal.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'modal',
  template: `
    <div>
      <div id="Modal"></div>
    </div>    
  `,
  styles: []
})
export class ModalComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

Upvotes: 2

Related Questions