Angular 角度:无法绑定到事件,例如在动态加载的组件上单击和更改
我已经创建了两个5角组件,它们是一个对话框和一个下拉列表 我想在对话框中打开下拉组件,所以我为它创建了一个对话框服务,并将其注入app.component。app.component中的代码如下-Angular 角度:无法绑定到事件,例如在动态加载的组件上单击和更改,angular,event-binding,angular-dynamic-components,Angular,Event Binding,Angular Dynamic Components,我已经创建了两个5角组件,它们是一个对话框和一个下拉列表 我想在对话框中打开下拉组件,所以我为它创建了一个对话框服务,并将其注入app.component。app.component中的代码如下- import { TestComponent } from './test/test.component'; constructor(private dialog: DialogService){} openDialog(){ this.dialog.open(TestComponent, {
import { TestComponent } from './test/test.component';
constructor(private dialog: DialogService){}
openDialog(){
this.dialog.open(TestComponent, {
modal: true
});
}
TestComponent非常简单,它在HTML文件中有组件选择器,如下所示-
<once-ui-dropdown
[options]="customLanguages"
[allowSearch]="true">
</once-ui-dropdown>
<div class="dropholder" (click)="this.openDropdown()"
...
...
...
其中customLanguages是一组选项
DialogService是一个简单的服务,它做两件事-
1)将对话框组件添加到主体
下面是代码片段-
DialogService.componentRef = this.componentFactoryResolver
.resolveComponentFactory(DialogComponent)
.create(this.injector);
this.appRef.attachView(DialogService.componentRef.hostView);
const domElem = (DialogService.componentRef.hostView as
EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
document.body.appendChild(domElem);
DialogService.componentRef.changeDetectorRef.detectChanges();
DialogService.componentRef=this.componentFactoryResolver
.resolveComponentFactory(DialogComponent)
.创建(此喷油器);
this.appRef.attachView(DialogService.componentRef.hostView);
const domElem=(DialogService.componentRef.hostView as)
EmbeddedViewRef)
.rootNodes[0]作为HTMLElement;
文件.正文.附件(domElem);
DialogService.componentRef.changeDetectorRef.detectChanges();
2:打开TestComponent/将其追加到div in对话框中
const loadComponentRef = this.componentFactoryResolver
.resolveComponentFactory(content)
.create(this.injector);
loadComponentRef.changeDetectorRef.detectChanges();
const domElem = (loadComponentRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
data = domElem.innerHTML;
document.getElementById('loadComponent').innerHTML = data;
const loadComponentRef=this.componentFactoryResolver
.resolveComponentFactory(目录)
.创建(此喷油器);
loadComponentRef.changeDetectorRef.detectChanges();
const domElem=(loadComponentRef.hostView作为EmbeddedViewRef)
.rootNodes[0]作为HTMLElement;
data=domElem.innerHTML;
document.getElementById('loadComponent')。innerHTML=data;
最后一件事嘿,你会在StackBlitz或类似的编辑器中创建一个编辑器吗?