Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!