【发布时间】:2018-09-04 05:36:37
【问题描述】:
在 component.ts 文件中,我正在像这样获取 json 数组中的表单字段值并将其转换为这样的 FormGroup 控件。这工作得很好。
getJsonForm(){
let base_url = 'example.org'
let getform_query = '/Base/formfieldsgenerator_get';
let getform_endpoint = base_url.concat(getform_query);
this.AllFormData = [];
this.http.get('getform_endpoint'.'?tabpgrpid='+this.tabpgrpid+'&tabgrpname='+this.tabgrpname+'&usertabgrpname='+this.usertabgrpname+'&moduleid='+this.moduleid+'&templateid='+this.templateid+'&all_mod_data='+this.all_mod_data,{headers: this.headers}).subscribe(
res => {
this.AllFormData = res;
// this array is used to iterate in html side
this.newfdata[element] = [];
this.newfdata[element]['properties'] = [];
Object.keys(res['com'][element]['schema']['properties']).forEach(inputKey => {
this.newfdata[element]['properties'].push(res['com'][element]['schema']['properties'][inputKey]);
});
// this is used to create form controls and form groups
this.objectProps = Object.keys(res['com'][element]['schema']['properties']).map(prop => {
return Object.assign({}, { key: prop} , res['com'][element]['schema']['properties'][prop]);
});
for(let prop of Object.keys(res['com'][element]['schema']['properties'])) {
formGroup[prop] = new FormControl(res['com'][element]['schema']['properties'][prop].value || '', this.mapValidators(res['com'][element]['schema']['properties'][prop].validation));
}
});
this.form = new FormGroup(formGroup);
});
}
现在在 components.html 方面,我正在使用这样的数组来生成动态表单。这也工作正常。
<form (ngSubmit)="custom_submit(form.value)" [formGroup]="form" >
<div *ngFor="let input1 of newfdata[tabname].properties">
<ng-container *ngIf="input1.type=='string'">
<div>
<mat-form-field>
<input matInput [formControlName]="input1.field_name" [id]="input1.field_name" type="text" placeholder="{{input1.title}}">
</mat-form-field>
</div>
</ng-container>
</div>
</form>
现在我想根据以前表单字段的更改来更改一个表单字段的值。为此,我无法订阅表单组变量的 valuechanges 发射器。
我已经在 ngOnit 上尝试过了,但它不起作用,并且在控制台中没有产生任何结果。
ngOnit(){
this.form.valueChanges.subscribe(val => {
this.formattedMessage = 'My changed values for is ${val}.';
console.log(this.formattedMessage);
});
}
编辑 1:
根据 Manzur Khan 的建议,我将值作为 true 传递给 formcreated 事件,然后在 ngOnit 中使用这样的值来获取 onchange 事件:
this.form = new FormGroup(formGroup);
this.dataService.change_current_form_created("true");
在 NgonIt 中
this.dataService.last_form_craeted_message.subscribe(data => {
if(data=="true") {
this.form.valueChanges.subscribe(val => {
this.formattedMessage = 'My changed values for is ${val}.';
console.log(this.formattedMessage);
});
}
});
现在我可以在控制台中登录更改事件,但无法获得 ${val} 的分辨率。
编辑 2:
由于 val 是对象,我无法以某种方式解析 ${val},我只是做了
this.form.valueChanges.subscribe(val => {
console.log('the changed value is',val);
});
它为我提供了给定表单组的所有值。我仍然需要进一步优化这个结果,以便我只听特定的表单控件。但它给了我一条可以走的路。谢谢大家。
【问题讨论】:
-
请将相关代码添加到您的查询中。由于额外的东西,它会混淆您的实际查询。
-
不要把代码放在 ngOnInit 中,就在你的行之后:this.form = new FormGroup(formGroup)。好吧,为了清楚起见,创建一个函数并在该行之后调用它
-
“现在我想根据以前表单字段的更改来更改一个表单字段的值”。好吧,考虑一下该值是否必须属于表单。如果你的“field”formattedMessage是“My ChangeValue”+input1.value,你不能有这个“field”,只是一个{{“My change Value”+form.getControl('input1').value}}
-
@mayur :这是让您了解整个场景的实际代码片段。这里没有什么是多余的。
标签: angular typescript events angular-reactive-forms eventemitter