I have a Angular Parent Component with tox Child Component. I want to share a variable between the two child and I configure this :
Parent
TypeScript
@Component({
selector: 'jhi-saison-dashboard',
templateUrl: './saison-dashboard.component.html',
})
export class SaisonDashboardComponent {
saisonSelectionnee?: ISaison;
evenementDeTopheader(saisonRetourneeParTopHeader: ISaison): void{
this.saisonSelectionnee = saisonRetourneeParTopHeader;
}
evenementDeOption(saisonRetourneeParOption: ISaison): void{
this.saisonSelectionnee = saisonRetourneeParOption;
}
}
HTML
<div>
<!-- Bandeau contenant la liste des saisons et les boutons de gestions-->
<jhi-saison-dashboard-top-header
[saisonSelectionnee]="saisonSelectionnee"
(emitEvent)="evenementDeTopheader(saisonSelectionnee!)">
</jhi-saison-dashboard-top-header>
<!-- Zone de gestion du paramétrage des jours/mois autorisés -->
<jhi-saison-dashboard-option
[saisonSelectionnee]="saisonSelectionnee"
(emitEvent)="evenementDeOption(saisonSelectionnee!)">
</jhi-saison-dashboard-option>
</div>
CHILD (same for Both)
Typescript
@Component({
selector: 'jhi-saison-dashboard-top-header',
templateUrl: './saison-dashboard-top-header.component.html',
})
export class SaisonDashboardTopHeaderComponent implements OnInit {
saisonIdActive?: number;
saisonIdSelectionnee?: number;
@Input() saisonSelectionnee?: ISaison;
@Output() emitEvent = new EventEmitter<ISaison>();
constructor(protected saisonService: SaisonService) {
}
/**
* Gestion des changements de saisons dans la liste déroulante
*/
changementSaisonDansListe(): void {
delete this.saisonSelectionnee;
if (this.saisonIdSelectionnee) {
this.saisonService.find(this.saisonIdSelectionnee).subscribe(
res => {
res.body ? this.saisonSelectionnee = res.body : delete this.saisonSelectionnee
this.emitEvent.next(this.saisonSelectionnee);
}),
() => {
// TODO : Gestion Erreur Subscribe
}
}
}
}
In the child, on the line with "this.emitEvent.next(this.saisonSelectionnee);", variable saisonSelectionee contain a object,
But when I'm return on the parent in the méthod "evenementDeTopheader", saisonRetourneeParTopHeader is null. Do you know why ?