You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So far we can completely create dynamic components, but we can't use ngOnChanges lifecycle since it doesn't react to @Input changes therefore we have to do this manually.
Another way to do this is to change the @Input field that concerned you to have getter and setter, so you can know when a change happens, but it is not a favorable option so let's stick ngOnChanges.
Let's start with creating changes object for the component.
Basically, do a loop over new inputs (currentInputs) and compare each input with the previous one, in case of change we add it as changed input to the changes object
Now, we have to manually call the ngOnChanges from the component instance if the component declared it and pass changes as an argument.
Changing the previous function to have the functionality
ngOnChanges(changes: SimpleChanges): void{// ensure component is definedassertNotNullOrUndefined(this.component);letcomponentChanges: Record<string,SimpleChange>;constshouldCreateNewComponent=changes.component?.previousValue!==changes.component?.currentValue||changes.injector?.previousValue!==changes.injector?.currentValue;if(shouldCreateNewComponent){this.destroyComponent();this.createComponent();// (1) componentChanges=this.makeComponentChanges(changes.inputs,true);}// (2)componentChanges??=this.makeComponentChanges(changes.inputs,false);assertNotNullOrUndefined(this.componentFactory);assertNotNullOrUndefined(this.componentRef);this.validateOutputs(this.componentFactory.outputs,this.outputs??{},this.componentRef.instance);this.validateInputs(this.componentFactory.inputs,this.inputs??{});// (3)if(changes.inputs){this.bindInputs(this.componentFactory.inputs,this.inputs??{},this.componentRef.instance);}// (4)if(changes.outputs){this.subscription.next();// to remove old subscriptionthis.bindOutputs(this.componentFactory.outputs,this.outputs??{},this.componentRef.instance);}// (5)if((this.componentRef.instanceasOnChanges).ngOnChanges){this.componentRef.instance.ngOnChanges(componentChanges);}}
Create changes object with firstChange as true after creating the component.
In case the component didn't change that means only the inputs or outputs did change so we create changes object with firstChange as false.
Rebind the inputs only if they did change.
Rebind the outputs only if they did change.
Calling component ngOnChanges lifecycle with the possible inputs changes.
The text was updated successfully, but these errors were encountered:
So far we can completely create dynamic components, but we can't use
ngOnChanges
lifecycle since it doesn't react to@Input
changes therefore we have to do this manually.Another way to do this is to change the
@Input
field that concerned you to have getter and setter, so you can know when a change happens, but it is not a favorable option so let's stickngOnChanges
.Let's start with creating changes object for the component.
Basically, do a loop over new inputs (
currentInputs
) and compare each input with the previous one, in case of change we add it as changed input to the changes objectNow, we have to manually call the
ngOnChanges
from the component instance if the component declared it and pass changes as an argument.Changing the previous function to have the functionality
firstChange
as true after creating the component.firstChange
as false.ngOnChanges
lifecycle with the possible inputs changes.The text was updated successfully, but these errors were encountered: