简体   繁体   English

Angular 7-ngForm,ngSubmit-提交旧值

[英]Angular 7 - ngForm, ngSubmit - submitting old values

I am using form in Angular 7 as below 我在Angular 7中使用表单如下

<form #payForm="ngForm" (ngSubmit)="onSubmit(payForm, $event)" method="POST" action="POST URL HERE">
      <input type="hidden" name="requestParameter" [value]="stringToSend" />
      <div class="text-center">
        <input type="submit" class="btn blbtn text-uppercase px-5 py-2 rounded my-3" value="Pay Now">
      </div>
      <div class="text-center">
        <a [routerLink]="['/logout']" class="onerem ltbl-link w-auto d-inline-block">Cancel</a>
      </div>
</form>

Here "stringToSend" is getting calculated each time there is some user action like different plan selection or coupon selection. 每当有一些用户操作(例如不同的计划选择或优惠券选择)时,都会计算出“ stringToSend”。 But when he finally clicks on "Pay Now" button, then I want to submit form to the POST URL from my angular component, so I am calling using ngForm with (ngSubmit)="onSubmit(payForm, $event)" 但是,当他最终单击“立即付款”按钮时,我想从我的角度组件向POST URL提交表单,所以我要使用ngForm并通过(ngSubmit)="onSubmit(payForm, $event)"调用

Below is my code from controller 下面是我的控制器代码

onSubmit(form: any, clkEvent: any): void {

// some database inserts before form submit
stringToSend = newCalculatedValue;   // setting stringToSend to new calculated value

clkEvent.target.submit();   // actual form submit from controller

}

But when I submit this form, the last value of stringToSend is getting send with form as form data not the latest one which I am calculating inside onSubmit() function. 但是,当我提交此表单时, stringToSend的最后一个值是使用表单作为表单数据发送,而不是我在onSubmit()函数中计算的最新表单。

Before setting latest value I consoled it, it's showing fine, but with form last value is submitting which is my Problem in angular. 在设置最新值之前,我已经对其进行了控制台,它显示良好,但是正在提交表单的最后一个值,这是我的问题。 I also tried [(ngModel)] but still no luck. 我也尝试过[(ngModel)]但还是没有运气。

Any help is appreciated. 任何帮助表示赞赏。

Thanks, Rohit 谢谢罗希特

The reason for not picking up latest value is how angular bindings work. 不使用最新值的原因是角度绑定的工作方式。 The input element is binded to stringToSend variable, but before angular gets a chance to update the bindings to the actual DOM node, the form gets submitted. input元素绑定到stringToSend变量,但是在angular有机会更新到实际DOM节点的绑定之前,必须提交表单。 One way to fix this issue, is to delay the form posting in the next event loop cycle (eg. using a setTimeout ). 解决此问题的一种方法是在下一个事件循环周期中延迟form发布(例如,使用setTimeout )。

onSubmit(form: any, clkEvent: any): void {

    // some database inserts before form submit
    stringToSend = newCalculatedValue;   // setting stringToSend to new calculated value
    setTimeout(()=>{
       clkEvent.target.submit();   // actual form submit from controller
    } , 0);
}

However, the above solution would a hacky way to fix the issue. 但是,以上解决方案将是解决该问题的一种简便方法。 Ideally, form submission should be delegated to a service. 理想情况下,应将表单提交委托给服务。

Refer to the docs for some best practices when dealing with form controls. 有关处理form控件的一些最佳做法,请参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM