Skip to content

Commit 7942c90

Browse files
Component doesn't have to be initialised anymore in ngOnChanges because connectInputsAndOutputs will automatically call the child component's ngOnChanges already
1 parent a5b7e2a commit 7942c90

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

src/app/shared/abstract-component-loader/abstract-component-loader.component.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, ComponentRef, Input, OnChanges, OnDestroy, OnInit, SimpleCha
22
import { Context } from '../../core/shared/context.model';
33
import { ThemeService } from '../theme-support/theme.service';
44
import { GenericConstructor } from '../../core/shared/generic-constructor';
5-
import { hasNoValue, hasValue, isNotEmpty } from '../empty.util';
5+
import { hasValue, isNotEmpty } from '../empty.util';
66
import { Subscription } from 'rxjs';
77
import { DynamicComponentLoaderDirective } from './dynamic-component-loader.directive';
88

@@ -57,21 +57,15 @@ export abstract class AbstractComponentLoaderComponent<T> implements OnInit, OnC
5757
* Set up the dynamic child component
5858
*/
5959
ngOnInit(): void {
60-
if (hasNoValue(this.compRef)) {
61-
this.instantiateComponent();
62-
}
60+
this.instantiateComponent();
6361
}
6462

6563
/**
6664
* Whenever the inputs change, update the inputs of the dynamic component
6765
*/
6866
ngOnChanges(changes: SimpleChanges): void {
69-
if (hasNoValue(this.compRef)) {
70-
// sometimes the component has not been initialized yet, so it first needs to be initialized
71-
// before being called again
72-
this.instantiateComponent();
73-
} else {
74-
if (this.inputNamesDependentForComponent.some((name: any) => hasValue(changes[name]) && changes[name].previousValue !== changes[name].currentValue)) {
67+
if (hasValue(this.compRef)) {
68+
if (this.inputNamesDependentForComponent.some((name: keyof this & string) => hasValue(changes[name]) && changes[name].previousValue !== changes[name].currentValue)) {
7569
// Recreate the component when the @Input()s used by getComponent() aren't up-to-date anymore
7670
this.destroyComponentInstance();
7771
this.instantiateComponent();
@@ -123,7 +117,7 @@ export abstract class AbstractComponentLoaderComponent<T> implements OnInit, OnC
123117

124118
/**
125119
* Connect the inputs and outputs of this component to the dynamic component,
126-
* to ensure they're in sync
120+
* to ensure they're in sync, the ngOnChanges method will automatically be called by setInput
127121
*/
128122
public connectInputsAndOutputs(): void {
129123
if (isNotEmpty(this.inputNames) && hasValue(this.compRef) && hasValue(this.compRef.instance)) {

0 commit comments

Comments
 (0)