我想創建嵌套表單,但我卡在第二級,不知道addAttribute和removeAttribute將如何看起來像?Angular2創建模型嵌套表格
export class ExportFormComponent implements OnInit {
public exportForm: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.exportForm = this.formBuilder.group({
dataType: [''],
items: this.formBuilder.array([
this.initItem(),
])
});
}
initItem() {
return this.formBuilder.group({
exportExpression: [''],
description: [''],
updatable: [true],
attributes: this.formBuilder.array([
this.initAttribute(),
])
});
}
initAttribute() {
return this.formBuilder.group({
exportExpression: [''],
localizedRawField: [''],
localizable: [true],
});
}
addItem() {
const control = <FormArray>this.exportForm.controls['items'];
control.push(this.initItem());
}
removeItem(i: number) {
const control = <FormArray>this.exportForm.controls['items'];
control.removeAt(i);
}
addAttribute() {
}
removeAttribute() {
}
save(exportConfiguration: ExportConfiguration) {
console.log(exportConfiguration);
}
}
我的界面樹
export interface ExportConfiguration {
dataType?: string,
items?: Item[],
}
export interface Item {
exportExpression?: string,
description?: string,
updatable?: boolean,
attributes?: Attribute[],
}
export interface Attribute {
exportExpression?: string,
localizable?: boolean,
localizedRawField?: string,
rules?: TransformationRule[]
}
export interface TransformationRule {
dataPathKey?: string,
expressionCheck?: boolean,
expression?: string,
}
編輯
好了,所以我使用的演示張貼的答案之一,但我下面的(this.itemsCtrl.get('${index}.attributes') as FormArray)
addAttribute(index: number) {
(this.itemsCtrl.get('${index}.attributes') as FormArray).push(this.initAttribute());
}
你能描述最終表單的控件樹的形狀,指示哪些部分是動態的? – AngularChef
@AngularFrance:我更新了。它有幫助 –