I have a child component which displays information when you click on its button.
information-data.component.html
<div class="col-12 col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-12 col-lg-12 p-2 info">
<h6 (click)="questionClick()">{{headName}}
<fa-icon [icon]="icons.faInfoCircle" class="fa-1x text-primary" transform="shrink-2"></fa-icon>
</h6>
</div>
<ng-template #childTemplate>
<div class="row" [hidden]="visibleStatus">
<div class="col-12 bulb-section">
<div class="row m-0">
<div class="icon-bulb col">
<p class="ml-4">
{{infoData}}
</p>
</div>
</div>
</div>
</div>
</ng-template>
<ng-container *ngIf="!displayInfo; then childTemplate"></ng-container>
</div>
information-data.component.ts
export class InformationDataComponent {
@ViewChild('childTemplate', { static: true }) childTemplate: TemplateRef<any>;
constructor() { }
@Input() infoData: string;
@Input() visibleStatus: boolean;
@Input() headName: string;
@Input() displayInfo?: boolean;
questionClick () {
this.visibleStatus = !this.visibleStatus;
}
}
Scenario – In my parent component, I have 3 bootstrap cards which have different data each. When I click on a card, I only want the information for the respective card to be displayed, hiding the other two in this scenario.
<div class="card">
<div class="card-body">
<information-data [infoData]="salonInfo"
[headName]="salonHead"
[visibleStatus]="true"
[displayInfo]="true"
></information-data>
</div>
</div>
</div>
<div class="col-4 col-sm-4 col-md-4 col-lg-4">
<div class="card">
<div class="card-body">
<information-data [infoData]="hospitalInfo"
[headName]="hospitalHead"
[visibleStatus]="true"
[displayInfo]="true"
></information-data>
</div>
</div>
</div>
<div class="col-4 col-sm-4 col-md-4 col-lg-4">
<div class="card">
<div class="card-body">
<information-data [infoData]="travelInfo"
[headName]="travelHead"
[visibleStatus]="true"
[displayInfo]="true"
></information-data>
</div>
</div>
<div class="row pl-5 pr-5">
<ng-container *ngTemplateOutlet="childComponent.childTemplate"></ng-container>
</div>
</div>
Other scenarios – I use this child component widely in other parts of the application as shown below, and it should not be changed.
<retail-information-data [infoData]="funcInfo"
[headName]="funcHead"
[visibleStatus]="funcVisibility"
></retail-information-data>
How can I go about implementing this functionality without affecting other parts of the code?
2
Answers
Use an Output
And when change
So, you can use two ways binding and your parent take account the change, define an array displayInfos
We can use the
EventEmitter
and@Output
to call the parent code, which will store the index of which element was clicked, then we can use aViewChildren
to get all the child components that are present, then use the set index, to access the template.I also use
*ngFor as syntax
to access the child component template!`
CHILD TS
CHILD HTML
MAIN.TS
Stackblitz Demo