Angular 角度2反应Froms-一次绑定整个对象
我正在学习Angular 2中的反应形式,我想知道是否有可能同时绑定所有对象属性。我看到的每一本指南都有这样的内容:Angular 角度2反应Froms-一次绑定整个对象,angular,typescript,Angular,Typescript,我正在学习Angular 2中的反应形式,我想知道是否有可能同时绑定所有对象属性。我看到的每一本指南都有这样的内容: this.form = this.fb.group({ name: ['', Validators.required], event: this.fb.group({ title: ['', Validators.required], location: ['', Validators.required] }) }); t
this.form = this.fb.group({
name: ['', Validators.required],
event: this.fb.group({
title: ['', Validators.required],
location: ['', Validators.required]
})
});
this.form.setValue({
name: object.name,
event: object.event
});
然后设置如下值:
this.form = this.fb.group({
name: ['', Validators.required],
event: this.fb.group({
title: ['', Validators.required],
location: ['', Validators.required]
})
});
this.form.setValue({
name: object.name,
event: object.event
});
对我来说,这在实际情况下是不可行的,因为您经常需要绑定很多属性,而定义它们并不是很方便。如果模型发生更改,则需要在两个位置对其进行更新
所以我试了一下:
this.refundRecordForm = this.fb.group(new RefundRecord());
this.refundRecordForm.setValue(this.refundRecord);
但我有一个错误:
错误:
尚未在此组中注册任何表单控件。如果您使用的是ngModel,
您可能需要检查下一个勾号(例如,使用setTimeout)
如果我使用patchValue
而不是setValue
,则没有错误,但表单不包含控件
我尝试手动绑定几个字段,但效果很好,所以我猜不能一次绑定整个对象
所以我想知道这是否可能。我对这个很陌生,所以可能我错过了一些明显的东西
你觉得怎么样?这行代码:
this.refundRecordForm = this.fb.group(new RefundRecord());
除非RETURNECORD被定义为一组表单控件,否则将不起作用
要使被动表单正常工作,您需要创建表单组和关联的表单控件。这就是您收到的错误消息的含义
您可以尝试按原样创建表单:
this.form = this.fb.group({
name: ['', Validators.required],
event: this.fb.group({
title: ['', Validators.required],
location: ['', Validators.required]
})
});
然后尝试使用快捷语法分配值:
this.form.setValue(this.refundRecord); // or .patchValue
我不知道这是否有效,我没有尝试过,所以让我们知道你是怎么做的。或者你可以建造一个plunker,我们可以试一试
您还可以看看这个:它显示了如何构建动态表单。这行代码:
this.refundRecordForm = this.fb.group(new RefundRecord());
除非RETURNECORD被定义为一组表单控件,否则将不起作用
要使被动表单正常工作,您需要创建表单组和关联的表单控件。这就是您收到的错误消息的含义
您可以尝试按原样创建表单:
this.form = this.fb.group({
name: ['', Validators.required],
event: this.fb.group({
title: ['', Validators.required],
location: ['', Validators.required]
})
});
然后尝试使用快捷语法分配值:
this.form.setValue(this.refundRecord); // or .patchValue
我不知道这是否有效,我没有尝试过,所以让我们知道你是怎么做的。或者你可以建造一个plunker,我们可以试一试
您还可以看看这个:它展示了如何构建动态表单。这里您需要做的只是创建一个函数,它将返回一个自定义类型的
FormGroup
。其他一切都可以保持不变
例如,更改以下内容:
this.refundRecordForm = this.fb.group(new RefundRecord());
this.refundRecordForm.setValue(this.refundRecord);
为此:
initRefundRecord(record: RefundRecord = new RefundRecord()): FormGroup {
return this.fb.group({
name: [record.name, Validators.required],
event: this.fb.group({
title: [record.event.title, Validators.required],
location: [record.event.location, Validators.required]
})
});
}
// Set to new empty form group of refund record type
this.refundRecordForm = this.initRefundRecord();
// Update the value later
this.refundRecordForm.patchValue(this.refundRecord);
使用这种方法,如果要使用预先存在的值创建表单,只需执行以下操作:
// Set to new prefilled form group of refund record type
this.refundRecordForm = this.initRefundRecord(preexistingRecord);
// Update the value later
this.refundRecordForm.patchValue(this.refundRecord);
一切都是一样的。现在,当您想要添加更多字段等时,您只需修改在
initreturnrecord
中创建的表单组。这里需要做的就是创建一个函数,该函数将返回自定义类型的FormGroup
。其他一切都可以保持不变
例如,更改以下内容:
this.refundRecordForm = this.fb.group(new RefundRecord());
this.refundRecordForm.setValue(this.refundRecord);
为此:
initRefundRecord(record: RefundRecord = new RefundRecord()): FormGroup {
return this.fb.group({
name: [record.name, Validators.required],
event: this.fb.group({
title: [record.event.title, Validators.required],
location: [record.event.location, Validators.required]
})
});
}
// Set to new empty form group of refund record type
this.refundRecordForm = this.initRefundRecord();
// Update the value later
this.refundRecordForm.patchValue(this.refundRecord);
使用这种方法,如果要使用预先存在的值创建表单,只需执行以下操作:
// Set to new prefilled form group of refund record type
this.refundRecordForm = this.initRefundRecord(preexistingRecord);
// Update the value later
this.refundRecordForm.patchValue(this.refundRecord);
一切都是一样的。现在,当您想要添加更多字段时,您只需修改在initreureRecord
中创建的表单组