1

Hi I'm trying to render component view through ComponentFactoryResolver and put to the DOM, but properties are undefined on render, what to do in that case?

property in component:

@Input('productId') productId;

creating view and passing data to property:

const componentRef: any = this.componentFactoryResolver
  .resolveComponentFactory(component)
  .create(this.injector);
componentRef.instance.productId = data.productId;

and in template

<div>{{ productId }}</div>

productId is undefined

peter 73
  • 85
  • 1
  • 8

1 Answers1

2

Call

componentRef.changeDetectorRef.detectChanges();

after you modify properties to invoke change detection.

See also https://angular.io/api/core/ComponentRef#changeDetectorRef

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • @peter 73 See also the 5 paragraph from https://stackoverflow.com/questions/40922224/angular2-component-into-dynamicaly-created-element/40926110#40926110 – yurzui Feb 28 '18 at 10:35