I need help as I am a bit lost right now. So, I have a component that dynamically injets a child component into its template using componentFactoryResolver , here is my HTML
<div class="dialog">
<div #container></div>
<button (click)="move('back')">Back</button>
<button (click)="move('forwards')">Forwards</button>
</div>
also in my component I have an observable that captures the click of the buttons like so, here is my (edited / reduced) code
// parent-component.ts
@ViewChild('container', {read: ViewContainerRef})
public dialogContainer: ViewContainerRef;
public navigationClick$: Observable<string> = new Subject<string>();
// click event on buttons
public move(direction): void {
this.navigationClick$.next(direction);
}
// code to inject child dynamic component, please ignore the args / variables
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.data.component);
this.componentRef = this.dialogContainer.createComponent(componentFactory);
this.embeddedcomponent = this.componentRef.instance as IWizardDialog;
this.embeddedcomponent.data = this.data.data;
Now I would like to pass the latest observable value from navigationClick$ to the child component I amend the code in my parent component thus...
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.data.component);
this.componentRef = this.dialogContainer.createComponent(componentFactory);
// NEW CODE
// here I subscribe to the observable and try to pass the value to the child component
this.navigationClick$.subscribe((data: string) => {
this.componentRef.instance.direction = data;
});
this.embeddedcomponent = this.componentRef.instance as IWizardDialog;
this.embeddedcomponent.data = this.data.data;
The subscribe is working in the parent as I would expect however I am unsure how I would capture / pass the subscribe data to the child component, for example can I just declare this as a Input()
// child-component.ts
@Input() public direction: string;
However this will just be undefined and isn't what I need. How do I pass the direction data from the subscribe to the child component or what code / feature do I need to receive the event / direction string? Any advice is appreciated.
If my wording is bad or confusing please say and I will rework the question.