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

194
Visualizações
Angular: Best way to pass data to a component (object vs many arguments)

I'm wondering which of the following is better

<my-component [data]="settings"></my-component>

or

<my-component 
    [bar]="settings.foo.bar"
    [baz]=settings.baz"></my-component>

And the settings object would then look like this

this.settings = {
    foo: { bar: 10 }
    baz: 2
};

The first form is compact, but less explicit about what my-component needs. I can image that there are situation in which it could be better to pass whole objects to a component (if there are too many properties for example). And what about performance. I can image that change detection is harder and less performant if you pass around whole objects. Any help/tips regarding this subject would be appreciated!

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I think specifying individual values is a better approach. As you wrote, it's more explicit about what you need, and you can specify the values from other sources (not just the settings object). This way, change detection is easy and your component can be optimized by using

@Component({  
  changeDetection: ChangeDetectionStrategy.OnPush
})

Using the OnPush strategy, Angular will skip the change detection for the component's subtree if none of its input values changed.

For more info see

  • https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html
  • https://blog.thoughtram.io/angular/2017/02/02/making-your-angular-app-fast.html
about 4 years ago · Santiago Trujillo Relatório

0

As far as my experience is concerned, the priority in front end software development today should be on code readability. As in terms of performances the difference is neglectable, I would opt for the first option, where you clearly pass a single settings object. When reading the .html page, there is no interest in digging in further here; the 'reader' can see the structure of your variable somewhere else in the script.

about 4 years ago · Santiago Trujillo Relatório

0

Better option is first. HTML code is more concise, when someone will need to find out what is settings, then no problem jump to ts file. Typescript types are a good way to identify objects, so you don't need to pass properties separately.

about 4 years ago · Santiago Trujillo 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