1

我的 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>

我试图将输入绑定到用户类属性,但在加载时出现错误

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:无法读取未定义的属性“用户名”我应该使用 ngModel 还是不需要它?

4

0 回答 0