角度表单不会发布到外部URL

Mat*_*t M 2 angular2-forms angular

我正在制作一个网站,该网站的表单需要对外部付款提供商进行POST。但是,我需要检查组件的代码以检查用户是否首先经过身份验证。如果没有,我需要通过我的webapi来更新值。收到响应后,我可以继续对外部URL进行POST。我的代码如下所示:

 onSubmit(form: any): void {  
         //for anonymous orders, we need to store the email before we send the form
         if(this.loggedIn){
            form.submit(); 
         }
         else{
             this.cartService.updateUserEmail(this.cart.id, this.email)
                             .subscribe(() => form.submit())
         }
  }
Run Code Online (Sandbox Code Playgroud)

如果this.loggedIn为true,则POST可以正常工作。仅当我需要调用cartService时,form.submit才起作用。有时,控制台显示错误,“对象不包含方法commit()”,有时,控制台中什么也没得到...。

这是开幕表格标记:

<form #cartForm="ngForm" (ngSubmit)="onSubmit(cartForm)" method="post" [action]="cart.paymentUrl" >
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。

编辑 这是我的提交按钮的标记,根据要求:

<button type="submit">CHECKOUT</button>
Run Code Online (Sandbox Code Playgroud)

Ste*_*mez 7

ngForm发送到您的事件处理程序之中没有一个.submit()方法,所以你需要得到底层form无论从ngFormevent因此你可以使用它的.submit()方法。

在此示例中,我使用了event

标记

<!-- Note that I added the $event to the handler -->    
<form #cartForm="ngForm" (ngSubmit)="onSubmit(cartForm, $event)" method="post" [action]="cart.paymentUrl" >
Run Code Online (Sandbox Code Playgroud)

TS

onSubmit(form: any, e: any): void {  
     //Note that I added 'e' and calling the event target's .submit()
     if(this.loggedIn){
        e.target.submit();
     }
     else{
         this.cartService.updateUserEmail(this.cart.id, this.email)
            .subscribe(() => {
                e.target.submit();
            })
     }
}
Run Code Online (Sandbox Code Playgroud)

我将其精简了一下,但这是一个正在工作的柱塞(例如,“正在工作”,console由于跨域问题,您会在www.google.com上注意到失败的POST-1秒后进入)。