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

149
Visualizações
How to get data into html in angular

I am trying to use angular to send data from parent to child component. In my parent component I use a onclick event to send data to my child component.

In my child component user-profile.ts i have this:

@Input('user')
  set user(user: any) {
  
  }

I know that the component recieved the data because if i console.log(user) it shows the data. However it doesnt see the data in the html. In the html of this file i have this

<h1>
  {{ user.name }}
</h1>

But i then get the error: "Cannot read property 'name' of undefined". What logic do i put into my .ts file to fix this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use below in your user-profile.ts

@Input() user: any; // or use your User type

and in your child component html file you can keep your change. To avoid undefined errors you can use. In your html.

{{user?.name}}
about 4 years ago · Juan Pablo Isaza Relatório

0

You haven't written the get statement

private _user; //Always defined the type instead using the any 
@Input('user')
set user(value: any) {
    this._user = value;
}

get user() {
  return this._user;
}

and also check null handle in user property, if data delayed.

    //HTML
   <h1>
   {{ user?.name }}
   </h1>
about 4 years ago · Juan Pablo Isaza Relatório

0

Please make sure you had pass the correct object and it not undefined. Or the best way is have it own default value.

about 4 years ago · Juan Pablo Isaza Relatório
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