I got an Angular parent component that use to pass a object to the child like the following
export class ParentComponent implements OnInit {
viewModel = {} as MyViewModel;
constructor(private service: MyService) {}
ngOnInit() {
this.service.subscribe((data) => {
this.viewModel = data.fdForms.viewModel;
});
}
I pass the populated viewModel object to the component thru the html
<app-table [viewModel]="viewModel"></app-table>
In my child component, it is passed with @Input()
export class TableComponent implements OnInit {
@Input() viewModel = {} as any;
constructor() { }
ngOnInit(): void {
console.log(this.viewModel); // this stays null even when parent is updated
}
}
After testing, the parent component get the updated data when it changes but the child component is not passed the updated object.
After googling, I think I need to change the code to make the child component subscribe to the parent viewModel object property.
UPDATE: trying OnChanges but getting error
export class TableComponent implements OnChanges {
@Input() viewModel = {} as any;
constructor() { }
ngOnChanges(changes: SimpleChange) : void {
console.log(this.viewModel); // this stays null even when parent is updated
}
}
But I am getting this error. I check other post on OnChanges, can't tell what is the error
Error TS2416: Property 'ngOnChanges' in type 'TableComponent' is not assignable to the same property in base type 'OnChanges'.
Type '(changes: SimpleChange) => void' is not assignable to type '(changes: SimpleChanges) => void'.
Types of parameters 'changes' and 'changes' are incompatible.
Type 'SimpleChanges' is missing the following properties from type 'SimpleChange': previousValue, currentValue, firstChange, isFirstChange
28 ngOnChanges(changes: SimpleChange) {
Thanks for any help from a newbie.