I have an interface User:
export interface User{
name:string;
email:string:
address:string;
}
I imported this on my ionic page and in the class, before the constructor code I have the code:
user:User;
Then, in the ngOnInit method I have the code that sets the value of this user variable.
This works OK.
Now I am trying to implement two-way binding of the user object and the form input fields. On the form input fields I use the model like this: [(ngModel)]="user.name"
Now I get an error that the user.name field is undefined. So I go to my page code and try to set user:User={};
This gives error because interface is expecting name, email and address - so cant be declared as empty. If I remove the interface and declare the code as: user:any={}
then it works OK.
I think that the undefined error is because the user object doesn't have value (set in the ngOnInit method) when the UI is being loaded? How do I go about such a situation where in I want to implement the concept of interface? Or is it good to implement nullable fields? Or is the best practice to use a class? Please I need some clarity on this.