I have a component inside another component, added by tag. At some point, i would like to reinitialize this sub-component, like the first it was invoke. Is there any way to do that?
You can re initialize by making the flag false and true . Can use a ChangeDetectorRef to trigger detection of the toggling without setTimeout e.g. this. flag = false; this. cd.
Technically, we do this by feeding the new value in Subject, by just calling next(newValue) , and then it can be sent to all Observers who then need to subscribe it. Taking an example where a message - a string is sent/updated by one component and the other component needs to listen to it and receive/update the same.
Do it manually. The very basic approach works by wrapping the element you want to rerender inside a ng-template element that gets rendered into a ng-container . On rerender you can just clear the content of the ng-container and create a new component from the ng-element .
You can write your own cleanup
method. But faster way (but not trully convenient) is to use *ngIf
. When value is false
, component is completely removed (destroyed) from page. When it is back to true
it goes via regular path constructor > ngOnInit
, etc. Why it is not convenient?
.
reinitChildComponent(): void{ this.childVisible = false; this.changeDetectorRef.detectChanges(); this.childVisible = true; this.changeDetectorRef.detectChanges(); }
You can re initialize component using *ngIf
.
<mychild *ngIf="flag"></mychild>
You can re initialize by making the flag false
and true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With