单击行后插入动态创建的组件 [英] Insert dynamically create component after clicked row
问题描述
我正在研究一种解决方案,在该解决方案中,我想在单击行后追加动态创建的组件
I am working on solution in which i want to append dynamically created component after clicked row
我的表包含带有操作按钮的行,单击这些按钮后,我将调用角度函数并在该行之后加载组件.
I have table consist rows with action button on click of which i will call angular function and load component after that row.
这是表代码
<div class="row" *ngFor="let rData of reportData; let i = index;" >
<div class="col" >
<button class="btn btn-sm" (click)="loadChildComponent()">+</button>
</div>
<div class="col">Name</div>
<div class="col">Description</div>
<ng-template #dynamic></ng-template>
</div>
动态组件代码
Service.ts
import { DynamicComponent } from './dynamic.component'
@Injectable()
export class Service {
factoryResolver;
rootViewContainer;
constructor(@Inject(ComponentFactoryResolver) factoryResolver) {
this.factoryResolver = factoryResolver
}
setRootViewContainerRef(viewContainerRef) {
this.rootViewContainer = viewContainerRef
}
addDynamicComponent() {
const factory = this.factoryResolver
.resolveComponentFactory(DynamicComponent)
const component = factory
.create(this.rootViewContainer.parentInjector)
this.rootViewContainer.insert(component.hostView)
}
}
这是组件文件.
dynamic.component.ts
dynamic.component.ts
import { Component } from '@angular/core'
@Component({
selector: 'dynamic-component',
template: `<div class="row" >
<div class="col">Data</div>
<div class="col">Data</div>
<div class="col">Data</div>
<div class="col">Data</div>
<div class="col">Data</div>
<div class="col">Data</div>
<div class="col">Data</div>
<ng-template #dynamic></ng-template>
</div>`
})
export class DynamicComponent { }
用于呈现动态组件的功能
Functions used to render dynamic component
@ViewChild('dynamic', {
read: ViewContainerRef
}) viewContainerRef: ViewContainerRef
loadChildComponent() {
this.service.setRootViewContainerRef(this.viewContainerRef)
this.service.addDynamicComponent()
}
现在将其添加到同一div中的任何行中
Right now its appending in same div for any of rows
我想在点击行后追加它
请帮助.
推荐答案
Angular中的ng-template
就像一个幽灵元素,即从不直接显示.选中此链接.
The ng-template
in Angular acts like a ghost element i.e. it is never displayed directly. Check this link.
更新:
因为您一直使用 @ViewChild
,所以总是在第一行插入模板. @ViewChild在模板中查找第一个元素.
You are having the template inserted with first row always because you have used @ViewChild
. @ViewChild looks for the first element in the template.
尝试使用 @ViewChildren
.
请参考以下更改:
<ng-container *ngFor="let rData of reportData; let i = index;">
<div class="row">
<div class="col" >
<button class="btn btn-sm" (click)="loadChildComponent(i)">+</button>
</div>
<div class="col">Name</div>
<div class="col">Description</div>
</div>
<div class="row">
<ng-template #dynamic></ng-template>
</div>
</ng-container>
JS更改:
@ViewChildren('dynamic', { read: ViewContainerRef }) viewContainerRef: QueryList<ViewContainerRef>
loadChildComponent(index) {
this.service.setRootViewContainerRef(this.viewContainerRef.toArray()[index])
this.service.addDynamicComponent()
}
希望这会有所帮助:)
这篇关于单击行后插入动态创建的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!