angularjs - Angular 2 - Create Component Dynamically - Is it mandatory to call 'detach()' method on the component reference? -
i have angular 2 app need create child components dynamically.
is mandatory call 'detectchanges()' , 'detach()' method on component reference variable 'componentref.changedetectorref' ?
i see things work if dont use them.
are these methods meant component injection performance improvement ?
@component({ selector: 'container', template: '<template #content></template>' }) export class containercomponet implements afterviewinit { contentcomponentref:any; @viewchild('content', {read: viewcontainerref}) contenthandle; constructor(private componentresolver:componentresolver) { super(); } ngafterviewinit() { if (this.contentcomponentref) this.contentcomponentref.destroy(); this.componentresolver.resolvecomponent(childcomponent) .then((factory:componentfactory<any>) => { let componentref = this.contenthandle.createcomponent(factory); componentref.instance['child_component_property'] = 'dummy value child'; componentref.changedetectorref.detectchanges(); componentref.ondestroy(() => { componentref.changedetectorref.detach(); }); this.contentcomponentref = componentref; return componentref; }); } }
Comments
Post a Comment