这是一个带有angular6的简单表单组:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
import { Subscription } from 'rxjs';
import { FormGroup, FormBuilder } from '@angular/forms';
import { User } from '../../commun/model/user.model';
@Component({
selector: 'app-newcontact',
templateUrl: './newcontact.component.html',
styleUrls: ['./newcontact.component.css']
})
export class NewcontactComponent implements OnInit {
userForm: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.initForm();
}
initForm() {
this.userForm = this.formBuilder.group({
firstName: '',
lastName: '',
email: ''
});
}
onSubmitForm() {
const formValue = this.userForm.value;
const newUser = new User(
formValue['firstName'],
formValue['lastName'],
formValue['email']
);
}
<form [formGroup]="userForm" (ngSubmit)="onSubmitForm()">
</form>
这是我的模特用户:
export class User {
constructor(
public firstName: string,
public lastName: string,
public email: string
) {}
}
我有一个错误,firebug说无法绑定表单组,我在我的代码片段中忘记了什么? 使用visual sudio,我没有错误。
谢谢你的回复