I am new to Angular and trying to create a multi-window popup box in the application and using the same component for all windows and I am loading window using ComponentFactoryResolver.
I am able to load the multiple instances of a window without any problem as well as able to pass information to the new window including window header(User Name) information along with the message list and the user name stay unique as expected but however all the message are replacing with whatever is in the very last instance and replacing all message with instance of last one.
dynamic content loading code:
`loadSingleWindow(user_info)
{
const chatBoxFactory = this.componentFactoryResolver.resolveComponentFactory(WindowComponent);
const viewContainerRef = this.container;
// viewContainerRef.clear();
this.componentRef = viewContainerRef.createComponent(chatBoxFactory);
// this.chatService.joinChat(user_info);
const windowInstance = <WindowComponent>(this.componentRef.instance);
windowInstance.chatUser = user_info;
this.chatService.loadExistingMessage('').subscribe(data => {
windowInstance.messages = data;
});
}
destroyComponent() {
this.componentRef.destroy();
}
`
WindowComponent looks like below.
export class ChatWindowComponent implements AfterViewInit {
public messages: Array<ChatMessage> = [];
activeRoom: any;
public chatUser;
ngAfterViewInit() {
console.log('hello world');
}
}
In the view of WindowComponents I am looping messages object to show all message.
Image with two instances of the same component with a unique header but message part is replacing by whichever load in the last:
