2

I am adding child component dynamically using ComponentFactoryResolver and able to using ngModel in the child component but it's working as a one way data binding rather than two way.

I tried by adding child component tag and it's working but in my case I can't add that. So need a solution with ComponentFactoryResolver. Please check the below stackblitz for better understanding. i need the parent component vlue to be update on second text box value change.

Example Stackblitz.

const factory = this.componentFactoryResolver.resolveComponentFactory(AppChildComponent);
const component = factory.create(this.ViewContainerRef.parentInjector);
component.instance['childData']=this.parentData;
this.ViewContainerRef.insert(component.hostView);
devDan
  • 5,969
  • 3
  • 21
  • 40
swarup
  • 159
  • 3
  • Possible duplicate: https://stackoverflow.com/questions/42848369/angular-passing-data-back-from-dynamic-component – Indragith Apr 16 '19 at 13:08
  • You need to convert the dynamically created component variable as an observable. Then you can listen for the changes in the main component – Indragith Apr 16 '19 at 13:09
  • 2
    I suggest you use an object, not a string, so. When pass data, pass the reference and change in child,change the property, see https://stackblitz.com/edit/angular-umds2q?file=app%2Fapp.component.ts – Eliseo Apr 16 '19 at 13:09

0 Answers0