Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
Child component Input () data is undefined when loaded

I have a parent component and that needs to be loaded multiple child components. The child components are to be loaded by taking the object from parent, so far its working as expected. But the problem here is, I have a button to be rendered in the child component and the text of the button comes from parent component. If I click on that button the respective child component should open and the data which came from parent component should be shown in the child component, here whenever I click on child component button, the child component is getting opened but the input() data is getting undefined. Because of this the data from the parent component is not displaying in the child component when it is expanded. Here is the sample code :

parentcompoent.html :

<div *ngFor="let tile of tiles$ | async;">
<ng-container [ngTemplateOutlet]="$any(getSelectedComponentTemplete(tile.tileUrl))"
                    [ngTemplateOutletContext]="{ $implicit: {data:tile} }">
</ng-container>
</div>

<ng-template #card let-obj>
<app-child [data]="obj.data"></app-child>
</ng-template>`

parentcomponent.component.ts :

@ViewChild('card', { static: true }) card!: TemplateRef<any>;

getSelectedComponentTemplete(type: string) {
switch (type) {
case "card":
return this.card;
default:
return;
}

childcomponent.html :

<mat-card>
<mat-card-title>
<button [routerLink]="data?.cardUrl" [disableRipple]="true" mat-flat-button>{{data?.tileName}}</button>
</mat-card-title>
</mat-card>`

childcomponent.component.ts :

@Input()
data!: any;

ngOnInit(): void {
console.log(this.data);
}

I don't know why its loading perfectly when the child component is part of parent component and its not loading data when I click on the button in child component.

Can someone help me on this ?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda