【发布时间】:2018-12-13 20:06:26
【问题描述】:
我正在尝试从 TS 文件中的表单访问数据。 我收到错误:RROR TypeError: Cannot read property 'name' of undefined
我的代码是这样的:
import { Component, OnInit } from '@angular/core';
import { employee } from '../models/Employee';
import { NgForm } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-employee-form',
templateUrl: './employee-form.component.html',
styleUrls: ['./employee-form.component.css']
})
export class EmployeeFormComponent implements OnInit {\
data: employee;
constructor() { }
ngOnInit() {
}
onSubmit(form:NgForm ) {
alert("Hello " + JSON.stringify(this.data));
}
}
<div class="container">
<form #form="ngForm" (ngSubmit)="onSubmit(form)">
<div class="form-group">
<label for="form">name</label>
<input type="text" class="form-control" id="name" [(ngModel)]="data.name" required>
</div>
</div>
<button type="submit" class="btn btn-success" >Submit</button>
</form>
</div>
我没有发现我的错误,有什么想法吗?
谢谢
【问题讨论】:
-
“数据”包含什么?
-
开头是空的。我想将输入传递给“数据”
-
请查看更新后的答案
标签: html angular angular-ngmodel