【发布时间】:2018-09-26 01:24:00
【问题描述】:
为什么我在组件中定义了Cannot read property 'UserName' of undefined 的错误。在 html 代码中使用“elvis 运算符”并不能解决问题。请查看我在哪里出现此错误的代码:
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>
使用 elvis 运算符:
<input class="validate" type="text" name="UserName" #UserName="ngModel" [(ngModel)]="user?.UserName" required minlength="3">
【问题讨论】:
-
那么您希望用户数据来自哪里?
标签: angular