3

I generate a compnent dynamically

var componentFactory = this.componentFactoryResolver.resolveComponentFactory(TransportOrderComponent);

let dynamicComponent = this.container.createComponent(componentFactory);

The bindings of the properties within the dynamic compoment don't work. (e. g. ngIf, button click, ...)

Component html:

<button (click)="show = !show">toggle: {{show ? 'hide' : 'show'}}</button>
<br>
<div *ngIf="show"> Text to show</div>

Component TypeScript:

...
export class TransportOrderComponent {
  show: boolean = true;
}

As static component everything works fine. If I add the dyn component and click the static components button the content of the dynamic component changes once

enter image description here

Here is a plunkr

1 Answer 1

3

That's because you're running code outside angular zone.

This should work for you

constructor(private zone: NgZone, ...) {}

addTransportOrderComponent(jqContainer: any) {
  this.zone.run(() => {
     let componentFactory = this.componentFactoryResolver.resolveComponentFactory(TransportOrderComponent);
     let dynamicComponent = this.container.createComponent(componentFactory);
  })
}

Modified Plunker

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.