2017-03-09 32 views
0

以下是jQuery的是提交一個表單的編程Angular2提交POST程序

$('#form').attr('action', window.siteRoot + 'Users/Cards/').submit(); 

我怎樣才能做同樣的Angular2?

以下代碼的問題在於它是Ajax,因此它需要返回響應。我不想回到新的觀點。

return this.http.post(GlobalVariables.SITE_ROOT + url, dto, { headers }) 
     .subscribe(function (data) { 
     console.log('received response'); 
    }); 

感謝很多

+0

只需要使用一個窗體而不是?如果你不需要執行異步請求,就避免這樣做。 – briosheje

+0

但我需要通過代碼提交表單... – Mark

回答

0

您需要使用路由器的POST完成之後重定向到新的。

export class ExampleComponent 
    constructor(private router: Router) {} 

    public function submitAndRedirect() { 
    return this.http.post(GlobalVariables.SITE_ROOT + url, dto, { headers }) 
     .subscribe(function (data) { 
      this.router.navigate(['newRoute', {id: id}]); 
    }); 
    } 
}