Well, your solution works just fine until the component changes its state and redistributes some things. Since I have not yet found the opportunity to get a ViewContainerRef for an element created outside of Angular (jquery, vanilla js, or even server-side) the first idea was to call detectChanges () by setting the interval. And after several iterations, finally, I came up with a solution that works for me.
Until 2017, you need to replace ComponentResolver with ComponentResolverFactory and do almost the same:
let componentFactory = this.factoryResolver.resolveComponentFactory(componentType), componentRef = componentFactory.create(this.injector, null, selectorOrNode); componentRef.changeDetectorRef.detectChanges();
After that, you can emulate the attached instance of the component to the change detection cycle by subscribing to the EventEmitters of your NgZone:
let enumerateProperties = obj => Object.keys(obj).map(key => obj[key]), properties = enumerateProperties(injector.get(NgZone)) .filter(p => p instanceof EventEmitter); let subscriptions = Observable.merge(...properties) .subscribe(_ => changeDetectorRef.detectChanges());
Of course, do not forget to unsubscribe from the destruction:
componentRef.onDestroy(_ => { subscriptions.forEach(x => x.unsubscribe()); componentRef.changeDetectorRef.detach(); });
UPD after re-stacking
Forget all the words above. It works, but just follow this answer
source share