2016-11-30 25 views
1

將表單提交到Angular 2應用程序的最佳方式是什麼?即從一個非角度的網站,到一個Angular應用程序?將表單提交到Angular 2應用程序

你不能簡單地把應用程序的操作URL與method="GET"爲角2個使用「矩陣網址符號」,瀏覽器將添加表單字段作爲常規PARAMS(?=,= &)。

例如這種方法行不通,假設URL是一個角2應用:

<form method="GET" action="http://myangular2app.com/search;"> 
    <input name="q"> 
    <input type="submit" value="Go"> 
</form> 

瀏覽器將導航到一個URL像http://myangular2app.com/search;?q=mysearch當它需要http://myangular2app.com/search;q=mysearch(沒有問號)。

回答

1

您可以利用雙向與使用ngModel指令結合

<input name="q" [(ngModel)]='name'> 

您需要導入forms modules利用雙向綁定;以及對請求的HTTP模塊:

import { FormsModule } from '@angular/forms'; 
import { Http }  from '@angular/http'; 

使用此呼叫:

this.http.get("http://myangular2app.com/search;q="+mysearch) 

如果你是新的Angular2我會建議你花一點時間看的Angular2 tutorial 'Tour of Heroes'

這引導您創建一個簡單的Web應用程序,其中涉及(除其他外)形式,數據綁定和HTTP請求

+0

對不起,但這並沒有回答這個問題。 – jimbo2087

+0

好 - 讓我澄清。如果你使用表單模塊,你不需要擔心角度矩陣URL表示法 –

相關問題