总体上来说,我对离子和移动开发还很陌生,我正在从事一个项目,该项目具有在离子段内具有字符串值的属性。该离子段包含两个离子段按钮,一个按钮的值为“ 1”(是),另一个按钮的值为“ 2”(为否)。当用户点击“是”按钮时,如果用户点击“否”,则会启用注释区域。按钮,注释部分被禁用。使用ngModel将此属性值绑定到模型页面上的属性。然后,在提交有效负载时,将此属性值在JSON有效负载中提交给后端时,需要将其转换为数字值。我发现,如果仅将此属性声明为数字,则会破坏注释启用/禁用功能,并且在提交值时,它仍将属性作为字符串值发送。我正在寻找一种在提交JSON负载后将属性值从字符串转换为数字的方法。
这是我目前拥有的:
属性值,在名为formPS4588的模型页面上声明
public safeWorkPracticesWereDemonstrated: any = '0';
HTML文件
<ion-item-group>
<ion-item-divider class="form-header">
Safe Work Practices`
</ion-item-divider>
<ion-item>
<p>
S42 Safe work practices were demonstrated
</p>
<ion-segment [(ngModel)]="form.safeWorkPracticesWereDemonstrated">
<ion-segment-button value="1" >Yes</ion-segment-button>
<ion-segment-button value="2" (ionSelect)="tapSafeWorkPracticesNo()">No</ion-segment-button>
</ion-segment>
</ion-item>
<ion-item>
<ion-label stacked>Safe Work Practice Recognition</ion-label>
<ion-textarea autoresize placeholder="Safe Work Practice Recognition" [disabled]="disableSafeWorkPractices()"
[(ngModel)]="form.safeWorkPracticeRecognition"></ion-textarea>
</ion-item>
</ion-item-group>
TS文件
public disableSafeWorkPractices(): boolean {
return (this.form as FormPS4588).safeWorkPracticesWereDemonstrated !== '1';
}
private submitForm(): void {
console.log('submitForm()');
this.form.updateObservationTimeTo();
let safeWorkPracticesWereDemonstrated = this.formPS4588.safeWorkPracticesWereDemonstrated;
this.formPS4588.change(safeWorkPracticesWereDemonstrated);
this.observation.confirmSubmitted();
this.backendService.postObservation(this.observation, this.form).then((result) => {
console.log(result);
this.scheduledObservation.confirmSubmitted();
this.loadingCtrl.create({
content: result,
duration: 1000
}).present();
this.exitForm();
});
}
转换函数,在名为formPS4588的模型页面上声明
public change(safeWorkPracticesWereDemonstrated: any): void {
Number(safeWorkPracticesWereDemonstrated);
}
答案 0 :(得分:0)
您做错了什么,就是您实际上没有更改该值。 代替这个:
this.formPS4588.change(safeWorkPracticesWereDemonstrated);
您应该执行以下操作:
this.safeWorkPracticesWereDemonstrated = Number(safeWorkPracticesWereDemonstrated);
答案 1 :(得分:0)
我相信,当用户点击提交表单的提交按钮时,我可以通过为属性值绑定一个转换函数来实现解决方案。
<ion-item-group padding>
<button ion-button block [disabled]="!isObservationCriteriaMet()" (tap)="doSubmit(); change();">Submit</button>
</ion-item-group>
public change(): void {
(this.form as FormPS4588).safeWorkPracticesWereDemonstrated = Number((this.form as FormPS4588).safeWorkPracticesWereDemonstrated);
}