question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular: Passing down data using @Input()

See original GitHub issue

I have three component-- Component A, Component B and Component C. Component A wraps Component B. Component B wraps Component C. If I use @Input() in each component, will changes to options in the root component pass through the nested components to Component C? See the “pseudo code” below:

<component-a [options]="options">
  <component-b [options]="options">
    <component-c [options]="options">
    </component-c>
  </component-b>
</component-a>

I asked this question because when options changes in the root component of the application I need to do some filtering at each nested component. However, I have noticed some unusual behavior with a third-party component that doesn’t seem to receive options? I can elaborate further after the first part of this question is answered.

I believe the answer to the question is “Yes”. So here is the second part. How do I update <ng-select> when the components are wraps as follows:

<component-a [options]="options">
  <component-b [options]="options">
    <ng-select [items]="options" [(ngModel)]="selectedOption">
    </ng-select>
  </component-b>
</component-a>

When the documentation for <ng-select> says the following:

Change Detection

Ng-select component implements OnPush change detection which means the dirty checking checks for immutable data types. That means if you do object mutations like:

this.items.push({id: 1, name: 'New item'})

Component will not detect a change. Instead you need to do:

this.items = [...this.items, {id: 1, name: 'New item'}];

This will cause the component to detect the change and update. Some might have concerns that this is a pricey operation, however, it is much more performant than running ngDoCheck and constantly diffing the array.

So how do I ensure changes to options is reflected in <ng-select>?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
softwareishappinesscommented, Aug 15, 2018

Perfect! I understand now. 🥇

1reaction
softwareishappinesscommented, Aug 14, 2018

I have three components that we are composed of ng-select. It would be helpful if we could get a response. If we have to drop ng-select we will have lost significant work. Is there any way or place I can get assistance. We would consider employing you as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component Data Passing in Angular With @Input - Topcoder
Firstly, we have to create a custom property to pass the data into a component. This can be done via input binding, which...
Read more >
Passing data into Angular components with @Input
The first step to passing data into an Angular component is to create a custom property to bind to. This is done via...
Read more >
Sharing data between child and parent directives ... - Angular
A common pattern in Angular is sharing data between a parent component and one or more child components. Implement this pattern with the...
Read more >
Passing Data into a Component - Rangle.io : Angular Training
There are two ways to pass data into a component, with 'property binding' and 'event binding'. In Angular, data and event change detection...
Read more >
Angular Pass data to child component - TekTutorialsHub
learn how to Pass data to child component in Angular using @Input. Detect changes to the input using OnChanges life Cycle hook or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found