I have a model.ts
file.
export class MainClass {
someVar: string;
var2: string;
subClass: SubClass;
contact: ContactList;
}
export class SubClass {
subItem: {
item1: string;
item2: string;
item3: string;
}
constructor() {}
}
export class ContactList {
itemList: ListItems[];
constructor() {}
}
export class ListItems {
list1: string;
list2: string;
list3: string;
}
In my coding journey, I encountered a service.ts
file where critical setter methods reside to update the model.
constructor(private model: SomeModel) {}
//some code
updateList(param1, param2, param3) {
this.model.subClass.subItem.item1 = param1;//doesn't work
this.model.contact.item[0].list1 = param2;//doesn't work
this.model.someVar = param3;//works
}
While trying to enhance the functionality, I faced the obstacle of "Can't read property subItem of undefined" error message when attempting to update subClass and contact. It's clear that initializing the subItem and itemList within the model's constructor is necessary, but grasping the correct syntax seems to be challenging. How can I effectively initialize and modify the model structure?