Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular attach component to body

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);
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: turf greatcircle example 
Javascript :: pdfjs customizing viewer.html js event handler 
Javascript :: javascript convert array of objects to array of uri 
Javascript :: heroku h10 error aws 
Javascript :: package.json beginner 
Javascript :: javascript es6 quizes 
Javascript :: vscode coderunner does not find python library 
Javascript :: convert json in parse inputs azure function 
Javascript :: vuejs install ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 
Javascript :: how to check type of value in a java script file 
Javascript :: how to translate english to hindi after enter space using javascript 
Javascript :: json identifier with period 
Javascript :: youtbe trailer npm 
Javascript :: jquery not calling id from div called in ajax 
Javascript :: css errors in node js server 
Javascript :: application/ld+json react 
Javascript :: Checkbox not binding to scope in angularjs 
Javascript :: user attributes for custom elemets 
Javascript :: is typescript slower than javascript 
Javascript :: onclick show 10 elements 
Javascript :: javascript conditional evaluation 
Javascript :: xjavascript 
Javascript :: array con tridimensional javascript 
Javascript :: jqgrid add edit or add options 
Javascript :: Node Locking 
Javascript :: immutable to object javascript 
Javascript :: pass color json api 
Javascript :: Minimize DOM access - JavaScript 
Javascript :: threejs torus shape 
Javascript :: how to open same project on different devices vue js 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =