angular - 将组件加载到 HTML 元素
问题描述
是否有通过代码将组件加载到 HTMLElement ?我试过这个:
var note = $(`
<app-test></app-test>`);
$(container).append(note)
但它不工作,但当我尝试
var note = $(`
<h1>Hi</h1>`);
$(container).append(note)
它完美无缺。有什么问题?
解决方案
让我们试试这个首先,我们创建一个DomService
来帮助您从容器中附加/分离组件
import {
ApplicationRef,
ComponentFactoryResolver,
ComponentRef,
EmbeddedViewRef, Inject,
Injectable,
InjectionToken,
Injector
} from '@angular/core';
import {ComponentPortal} from '@angular/cdk/portal';
import {DOCUMENT} from '@angular/common';
export const MY_TOKEN = new InjectionToken<{}>('MY_TOKEN');
@Injectable({
providedIn: 'root'
})
export class DomService {
constructor(
@Inject(DOCUMENT) private document: Document,
private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
) {
}
private _createInjector(useValue): Injector {
return Injector.create({
providers: [{provide: MY_TOKEN, useValue}]
});
}
createComponent(
component: any,
appendTo: Element,
componentProps: {},
wrapperClass: string
): ComponentRef<unknown> {
const componentRef = this.componentFactoryResolver
.resolveComponentFactory(component)
.create(this._createInjector(componentProps));
this.appRef.attachView(componentRef.hostView);
const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
if (wrapperClass) {
const hasWrapper = appendTo.querySelector(`.${wrapperClass}`);
if (!hasWrapper) {
const divTag = this.document.createElement('div');
divTag.className = wrapperClass;
appendTo.appendChild(divTag);
}
appendTo = appendTo.querySelector(`.${wrapperClass}`);
}
appendTo.appendChild(domElem);
return componentRef;
}
detachComponent(componentRef: ComponentRef<unknown>): void {
this.appRef.detachView(componentRef.hostView);
componentRef.destroy();
}
}
其次,在您的组件中,您只需注入上述服务并使用它。
例如,您想追AppTestComponent
加到<div id="container"></div>
export class AppTestComponent {
name: string;
constructor(
@Inject(MY_TOKEN) public token: any
) {
this.name = token.name;
}
}
export class MyComponent implements OnInit, OnDestroy {
componentRef: ComponentRef<any>;
constructor(
@Inject(DOCUMENT) private document: Document,
private domService: DomService,
){}
ngOnInit() {
this.componentRef = this.domService
.createComponent(
AppTestComponent,
this.document.getElementById('container'),
{name: 'just for test'},
'app-test-container'
);
}
ngOnDestroy(): void {
this.domService.detachComponent(this.componentRef);
}
}
推荐阅读
- javascript - 用于 Observable 的 GroupBy RxJs
- javascript - 每次刷新页面时,我可以将json数据存储在数组中吗
- java - 为什么内部循环范围会在解决此问题的方式上有所不同?
- git - 使用换行间距格式化 git diff
- oracle - 如何从存储过程返回的游标将数据插入临时表
- c++ - 从 std::string 的两端删除子字符串?
- asp.net-mvc - 已发布应用程序出错但不是本地编译器错误消息:CS0012:
- r - 结合 R 中 geom_dotplot() 创建的三个不同的图?
- apache-nifi - 从 HDF 集群迁移到独立的 nifi
- c# - 未生成 ASP.NET MVC Identity 默认控制器和视图