Why do I keep on having the error of Cannot read property 'UserName' of undefined
when I have defined it in my Component. Using the 'elvis operator' in the html code does not do the trick. Please look at my code on where I get this error:
user.model.ts
export class User {
UserName: string;
Password: string;
Email: string;
FirstName: string;
LastName: string;
}
sign-up.component.ts
export class SignUpComponent implements OnInit {
user: User;
constructor() { }
ngOnInit() {
}
}
sign-up.component.html
<form class="col s12 white" #userRegistrationForm="ngForm">
<div class="row">
<div class="input-field col s6">
<input class="validate" type="text" name="UserName" #UserName="ngModel" [(ngModel)]="user.UserName" required minlength="3">
<label>Username</label>
</div>
<div class="input-field col s6">
<input type="password" name="Password" #Password="ngModel" [(ngModel)]="user.Password">
<label>Password</label>
</div>
</div>
<div class="row">
</div>
</form>
Using the elvis operator:
<input class="validate" type="text" name="UserName" #UserName="ngModel" [(ngModel)]="user?.UserName" required minlength="3">