Passing a parameter to a child component in the parent component:
<app-conf-name
[inputName]='person.name'
(updatedNameEvent)='updateName($event)'>
</app-conf-name>
Implemented within the TypeScript file of the component
@Input() inputName: Names;
@Output() updatedNameEvent: EventEmitter<Names> = new EventEmitter();
constructor() { }
editName: Names = new Names();
ngOnInit() {
this.editName = this.inputName;
}
The Names class in the code sets default values for various fields like correctPerson.
export class Names {
indivId: number;
firstName: string;
prefName: string;
lastName: string;
suffix: string;
updated: boolean = false;
correctPerson: boolean = false;
correctAsIs: boolean = false;
correctionDate: Date;
addedDate: Date;
}
Encountering an error message:
ERROR TypeError: Cannot read property 'correctPerson' of undefined
at Object.View_ConfNameComponent_0._co [as updateDirectives] (ConfNameComponent.html:9)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13056)
at checkAndUpdateView (core.es5.js:12236)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
The object person
is initialized through a service called in the parent component.
Parent Component
person: Person = new Person();
constructor(private peopleService: PeopleService) { }
state: string = 'one';
ngOnInit() {
this.peopleService.getPersonById(this.Id).subscribe((data)=>{
this.person = data;
console.log(this.person);
this.person.name.correctPerson = false;
this.person.name.correctAsIs = false;
this.person.email.correctAsIs = false;
this.person.phone.correctAsIs = false;
this.person.address.correctAsIs = false;
});
}
Note that I've re-initialized the property causing the error with a new assignment.
The original code for the child component used the input variable as the working variable.
Despite multiple attempts to resolve issues and debug, the final child component remains problematic even after adding ngIf statements for validation.
For further details, refer to the complete GitHub Repository