我可以做線沿線的東西:創建一個DOM元素, 其追加到身體,然後渲染的角度成分爲DOM 元素的子?
是的,你可以做到這一點。您需要使彈出組件動態並在創建時傳遞DOM元素。您還需要將結果視圖附加到ApplicationRef.views。這種方法類似於@angular/material CDK is using。
這裏是stackblitz demo和代碼:
import { Component, ComponentFactoryResolver, Injector, Inject, Renderer2, ApplicationRef } from '@angular/core';
import { PopupComponent } from './popup.component';
import { DOCUMENT } from '@angular/platform-browser';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 4';
constructor(
resolver: ComponentFactoryResolver,
injector: Injector,
@Inject(DOCUMENT) document,
renderer: Renderer2,
appRef: ApplicationRef) {
setTimeout(()=>{
const element = renderer.createElement('div');
renderer.appendChild(document.body, element);
const factory = resolver.resolveComponentFactory(PopupComponent);
const compRef = factory.create(injector, [], element);
appRef.attachView(compRef.hostView);
})
}
}
要了解更多有關動態組件閱讀: