add-employee.html
<div class="col-md-6 offset-md-3">
<h3> Create Employee </h3>
<form (ngSubmit) = "onSubmit()">
<div class="form-group">
<label>Name</label>
<input type="text" class ="form-control" id = "employeename"
[(ngModel)] = "employee.employeename" name = "employeename">
</div>
<div class="form-group">
<label>Age</label>
<input class ="form-control" id = "age"
[(ngModel)] = "employee.age" name = "age">
</div>
<button class = "btn btn-success" type ="submit">Submit</button>
</form>
</div>
add-employee.ts
import { Component, OnInit } from '@angular/core';
import { Employee } from '../employee';
import { EmployeeserviceService } from '../employeeservice.service';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-add-employee',
templateUrl: './add-employee.component.html',
styleUrls: ['./add-employee.component.css']
})
export class AddEmployeeComponent implements OnInit {
employee !: Employee;
constructor(private employeeService:EmployeeserviceService) { }
ngOnInit(): void {
}
onSubmit(){
console.log(this.employee);
this.employeeService.saveEmployee(this.employee).subscribe(data=>{
console.log(data);
})
}
}
我是新的Angular 和我试图创建一个样本员工CRUD应用程序的动手经验。但在创建POST API创建我得到:
无法读取属性employeename
我不知道我哪里做错了,有人能帮我解决这个问题吗?
先谢了。
1条答案
按热度按时间k4aesqcs1#
注解中提到的第一个问题是,在使用
Employee
示例之前,您没有将其初始化为employee
变量。将示例初始化为employee
变量可以解决此问题。要隐藏初始化年龄值(
0
),可以考虑将Employee
类中的age
属性应用为可空。并将
age
初始化为null
。