$ http通过表格获取请求

$ http通过表格获取请求

问题描述:

我根本找不到适合我的问题的答案。我对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

首先在所有你应该为了得到关于它的一个更好的主意阅读本$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

根据响应是对象还是数组,您可以使用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

你可以试试并使用$http.get(url,{value1: your.form.input})使http获得您的自定义值!