在Angular 2中使用反应形式时,一切都按预期工作,直到我开始使用数据模型。之后数据不再与我的孩子输入结合。
这就是我在app.component中设置内容的方法:
我可以在https://stackblitz.com/edit/angular-podknk
复制它import { Component, OnChanges, Input, Output } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators } from
'@angular/forms';
import { Parent, ChildGroup } from './test.model';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnChanges {
@Output() parent: Parent;
public myForm: FormGroup;
constructor(
private fb: FormBuilder,
) {
this.buildForm()
}
buildForm() {
this.myForm = this.fb.group({
parentInput: '',
childGroup: this.fb.group(new ChildGroup()),
});
}
ngOnChanges() {
this.myForm.reset();
this.myForm.setValue({
parentInput: this.parent.parentInput,
childInput: this.parent.childGroup,
})
}
save() {
console.log(this.myForm.value)
}
}