2014-09-23 142 views
1

我根本找不到適合我的問題的答案。我對Angular頗爲陌生,並且對$ http請求有一個簡單的問題。我有一個表單,用戶可以在其中輸入地點和時間,例如多個人。

現在我想使用這三個用戶輸入從我的服務器獲取數據,以提供用戶請求的答案。

<input type="text" placeholder="bla1" class="form-control" ng-model="formData.input1"> 
<input type="text" placeholder="bla2" class="form-control" ng-model="formData.input2"> 
<input type="text" placeholder="bla3" class="form-control" ng-model="formData.input3"> 

很明顯,我現在可以將它們全部發布到ng-submit格式數據。但是我想用這些信息來檢索數據併發出一個get $http請求。

我相信paramsdata屬性是要更改和利用的屬性。

有人可以向我解釋這將如何工作,或者一般情況下,表單的輸入如何用於指定$ http請求?

非常感謝大家!

+0

所以你想使用$ http.get(url)函數? – rllola 2014-09-23 14:23:31

回答

2

首先在所有你應該爲了得到關於它的一個更好的主意閱讀本$http;

對於使用獲取您將不得不建立您的查詢字符串與您的輸入後,您將添加到您的基本網址後。

var completeUrl = baseUrl + "?input1="+ formData.input1 
        + "&input2=" + formData.input2 //and the same for input 3 
$http({method: 'GET', url: completeUrl}). 
success(function(data, status, headers, config) { 
    // here data contains all informations returned by the server 
}). 
error(function(data, status, headers, config) { 
    //In case your server respond with a 4XX or 5XX error code 
}); 

您可以通過簡單地把它在一個範圍內函數調用這段代碼,並與一個叫NG提交

$scope.submitFunction = function() { .. } 

且模板中

<form ng-submit="submitFunction()" > 
+0

非常感謝。這對我對這個問題的理解非常有幫助.... – Dribel 2014-09-23 14:20:08

0

根據響應是對象還是數組,您可以使用getquery函數。

$http.get(url,{params:{'item1':formData.input1,'item2':formData.input2,'item3':formData.input3}});

這個數據會去查詢字符串。

url?item1=data&item2=data&item3=data

+0

非常感謝。它有助於讓我的問題得到控制。 ;) – Dribel 2014-09-23 14:20:28

0

你可以試試並使用$http.get(url,{value1: your.form.input})使http獲得您的自定義值!