简体   繁体   English

FormComponent.html:8错误TypeError:无法读取未定义的属性'userName'

[英]FormComponent.html:8 ERROR TypeError: Cannot read property 'userName' of undefined

I am working on a angular 7 and following the form.component.html code : 我正在研究angular 7,并遵循form.component.html代码:

{{ userForm.value | {{userForm.value | json }} json}}
{{ userModel | {{userModel | json }} json}}
  <div class="form-group"> <label>Name</label> <input type="text" class="form-control" name="userName" [(ngModel)]="userModel.userName"> </div> <div class="form-group"> <label>Email</label> <input type="text" class="form-control" name="email" [(ngModel)]="userModel.email"> </div> <div class="form-group"> <label>Mobile number</label> <input type="number" class="form-control" name="number" [(ngModel)]="userModel.number"> </div> <div class="form-group"> <label>Address</label> <input type="text" class="form-control" name="address" [(ngModel)]="userModel.address"> </div> <button class="btn btn-primary" type="submit">Submit Order</button> 

user.js code user.js代码

export class User {
    constructor(
        public userName: string,
        public email: string,
        public number: string,
        public address: string
    ){}
}

app.component.ts: app.component.ts:

import { Component } from '@angular/core';
import { User } from './User';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title=['Angular','Vue','React'];

  userModel = new User('ash','ash@gmail.com','01233','asasas');
}

When i compile i get the error "userName" undefined. 当我编译时,我得到未定义的错误“ userName”。

 <input formControlName="userName" placeholder="Name" class="form-control">

在每个输入字段中使用它,它将起作用。

Change your User class like below 如下更改用户类

export class User {
        public userName: string;
        public email: string;
        public number: string;
        public address: string;
    constructor(userName, email, number, address){
        this.userName = userName;
        this.email = email;
        this.number = number;
        this.address = address;
     }
}

have a look here for more about typescript classes. 在这里查看更多有关打字稿类的信息。

as @Hameed Syed said cross check once again for your class import statement. 正如@Hameed Syed所说,再次对您的课程导入声明进行交叉检查。

UPDATED 更新

your code seems fine for me, any how try to use Life Cycle methods from Angular for data binding. 您的代码对我来说似乎还不错,无论如何尝试使用Angular的生命周期方法进行数据绑定。

import { Component, OnInit } from '@angular/core';
import { User } from './User';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title=['Angular','Vue','React'];

  userModel: User;

  ngOnInit(){
    this.userModel = new User('ash','ash@gmail.com','01233','asasas');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误类型错误:无法读取未定义 Ionic/Firestore 的属性“用户名” - ERROR TypeError: Cannot read property 'username' of undefined Ionic/Firestore Angular 错误类型错误:无法读取未定义的属性“用户名” - Angular ERROR TypeError: Cannot read property 'userName' of undefined TypeError:无法读取password.js中未定义的属性“用户名” - TypeError: Cannot read property 'username' of undefined in passportjs 未捕获的类型错误:无法读取未定义的属性“用户名” - Uncaught TypeError: Cannot read property 'username' of undefined TypeError:无法读取未定义的属性“用户名”? - TypeError: Cannot read property 'username' of undefined? “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 类型错误:无法读取未定义的属性“用户名”-- - TypeError: Cannot read property 'username' of undefined-- 错误TypeError:无法读取未定义的属性“ html”(角字体) - ERROR TypeError: Cannot read property 'html' of undefined (angular-fontawesome) 无法读取未定义错误的属性“用户名” - Cannot read property 'username' of undefined error 类型错误:无法读取未定义的 discord.js 的属性“用户名” - TypeError: Cannot read property 'username' of undefined discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM