【发布时间】:2017-09-12 21:17:41
【问题描述】:
我有一个带有验证的Angular Reactive Form。
为我的hiddenComposedField 调用设置器的正确方法是什么?
component.ts
ngOnInit() {
this.myForm = this.formBuilder.group({
'field1': ['', [Validators.required]],
'field2': ['', [Validators.required]],
'hiddenComposedField': [''],
});
this.myForm.get('field1').valueChanges.subscribe((val) => {
this.setHiddenComposedField();
});
this.myForm.get('field2').valueChanges.subscribe((val) => {
this.setHiddenComposedField();
});
}
setHiddenComposedField() {
let field1 = this.myForm.get('field1').value;
let field2 = this.myForm.get('field2').value;
// This doesn't work, but I want something like it:
this.myForm.set('hiddenComposedField',field1 + ' ' + field2); // :(
}
component.html
<form [formGroup]="myForm">
<input formControlName="field1">
<input formControlName="field2">
<!-- NB: hiddenComposedField is not exposed to the user; -->
<!-- it is only on the submitted form model. -->
<button type="submit">Submit</button>
</form>
【问题讨论】:
标签: angular validation reactive-programming