Angular: Passing down data using @Input()
See original GitHub issueI 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:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
Perfect! I understand now. 🥇
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 dropng-select
we will have lost significant work. Is there any way or place I can get assistance. We would consider employing you as well.