You can wait for the user to click on the link and as soon as Click Event occurs, load the required component in the view.
Things to keep in mind:
- You need to have a placeholder defined for the component in the view.
The terms and conditions component needs to be defined as Entry level component for it's Module or the module where it is used.
entryComponents: [
ChildComponent
],
Make sure to refer to the placeholder in your component and attach the terms and condition component dynamically.
<div>
<ng-template #viewContainerRef></ng-template>
</div>
and
@ViewChild('viewContainerRef', { read: ViewContainerRef }) VCR: ViewContainerRef;
and create the component dynamically:
createComponent() {
let componentFactory = this.CFR.resolveComponentFactory(ChildComponent);
let componentRef: ComponentRef<ChildComponent> = this.VCR.createComponent(componentFactory);
let currentComponent = componentRef.instance;
currentComponent.selfRef = currentComponent;
// to track the added component, you can reuse this index to remove it later
currentComponent.index = ++this.index;
// providing parent Component reference to get access to parent class methods
currentComponent.compInteraction = this;
}
there is an example for you here: https://add-or-remove-dynamic-component-parent-child.stackblitz.io
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…