I have an Angular project in which i defined some classes Classroom and Student and Address.
The problem in Angular when i use the bidirectional binding in edit-student-component.html where an address reference is null or undefined it does not work anymore.
<span class="field mr-3">
<input pInputText id="student-address-street" placeholder="Student Street" [(ngModel)]="selected.address.street"/>
</span>
this code throws an error in case address is null or undefined, so i thought i would define setters and getters to get and set the student address.
export class Classroom {
id: number
students : Student[];
}
export class Student {
id: number
firstname: string
lastname: string
age: number
get address() : Address {
if (this.address) {
return this.address
} else {
return new Address()
}
}
set address(address: Address) {
if (address) {
this.address = address
} else {
this.address = new Address()
}
}
}
export class Address {
id: number
street: string
city: string
country: string
postcode: string
}
however when i do this i get a compile error in the student-edit-component.ts at this line:
const observer: Observer<Student> = {
next: (data: any) => {
...
this.students.splice(index, 1, {...this.editStudent}) <<<<<<< erronous line
// this.editStudent is the selected student which is beeing edited
...
TS2345: Argument of type ‘{ id: number; firstname: string; lastname: string; age: number;}’ is not assignable to parameter of type ‘Student’.
Type ‘{ id: number; firstname: string; lastname: string; age: number; }’ is missing the following properties from type ‘student’: address