将组件与Angular 6上的其他容器组件一起添加到主体

时间:2018-08-17 08:53:05

标签: angular

我已经接受了接受组件的服务,将该组件添加到我的组件中并将它们添加到<body>。问题是,当我在组件中放入ng-content来放置传入的组件时,它只是将其添加到ng-content下方,就像appendChild一样。

./ 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() {
  }
}

如果我们提供图片组件,则该服务调用的结果将显示在正文中

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

对身体的期望:

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

1 个答案:

答案 0 :(得分:2)

作为解决方案,您可以通过将传入DOM放入模式DOM内的特定标签中来执行javascript解决方案。 ./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() {
  }
}