我的HTML看起来像这样
<form>
<div class="input">
<label >Username</label>
<br>
<input [(ngModel)]="user.username" name = "username" type="text" id="username">
</div>
<div class = "input">
<label >Password</label>
<br>
<input [(ngModel)]="user.password" name = "password" type="password" id = "password">
</div>
<div class = "input">
<label >Confirm Password </label>
<br>
<input [(ngModel)]="passwordCheck" name = "passwordCheck" type="password">
</div>
<div >
<input type="submit" (click)="addUser(user)" value="Register">
</div>
</form>
我尝试将输入绑定到User类属性,但是我在加载时遇到错误
import { Component, OnInit } from '@angular/core';
import { User } from '../user';
import { OsobaService } from '../osoba.service';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
passwordCheck : string;
user : User;
constructor(private osobaService : OsobaService) { }
ngOnInit() {}
addUser(user : User)
{
this.osobaService.addUser(user);
}
}
我如何编码此控制台错误?
TypeError:无法读取undefined的属性“username” 我应该使用ngModel还是我不需要它?