I'm developing an Angular app and I would use ngOnChanges to get notified when the Input of a component changes. In my case it is not fired because I have an array of nested object as Input for the component.
I have a component with a form in which I use two sub-components that use two FormControl of the same parent FormGroup. I pass the value of the first formControl to the second sub-component, but since no change detection occurs on array modification I can't get the new values in the Input of the second sub-component.
Here is my code:
@Component({
encapsulation: ViewEncapsulation.None,
moduleId: module.id,
selector: 'my-component',
templateUrl: 'my-component.html',
styleUrls: ['my-component.scss']
})
export class MyComponent {
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = fb.group({
documents: null,
summary: null
});
}
}
Here is the template file:
<div class="content">
<form class="ui form" [formGroup]="myForm">
<my-component-child-one [form]="myForm.get('documents')"></my-component-one>
<my-component-child-two [form]="myForm.get('summary')" [content]="myForm.get('documents').value"></my-component-two>
</form>
</div>
Any change made in the form control of the first sub-component does not trigger ngOnChanges of the second sub-component, because it binds the array reference instead of array content. So any change made to any object of the array or any change made to the array itself doesn't change the array reference and then ngOnChanges() will not be called.
A possible solution may be using ngDoCheck. but it could have a frightful cost because it is called with enormous frequency. It becomes heavy even because I should use IterableDiffers and KeyValueDiffers.
Another solution could be subscribing to valueChanges of the first formControl, but I cannot figure out why it seems not to work propertly, sometimes it misses some changes...
What could be the best solution in my case?
OnPush?