Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda