I am creating Quora/facebook like comment functionality. But comments can go to any depth. Following is my template:
<ul>
<li *ngFor="let item of items let i = index">
<!-- some html code -->
<button(click)="addComment(item.id,i)">
<div>
<ng-template #target></ng-template>
</div>
</li>
</ul>
I am creating dynamic component and for this i have refered this answer.
export class myDynamicComponent {
@ ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef;
constructor(private replyService: ReplyService, private cfr:
ComponentFactoryResolver){}
public addComment(Id:any, index:any){
this.commentService.getContent(Id)
.subscribe(data => {
let compFactory =
this.cfr.resolveComponentFactory(myDynamicComponent);
let instance=this.target.createComponent(compFactory).instance;
instance.items=data ;
}
}
}
So i am creating dynamic component in #target. I have multiple because it is generating in the loop. The problem with this is that newly created component get attached to first #target and not the current (clicked) #target.
What is the way to solve this?