Angular:如何将$ scopevariables传递给Node.js服务器。
现在我有这样的forms:
<form ng-submit = "submit()"ng-controller = "formCtrl"> <input ng-model="formData.stickie" type="text" id="sticky_content" /> <button type="submit" id="add_sticky" value="add a new stickie!">new sticky</button> </form>
由这个模型控制:
app.controller('formCtrl',function($scope,$http){ $scope.submit = function() { $http.post('/api/stickies', $scope.formData) **** somehow assign data to something useable by the function below???****** }) .error(function(data){ console.log('Error: ' + data); }); }; });
我希望能够在这里使用发布的数据:
app.post('/api/stickies',function(req,res){ var test = formData.stickie; //returns undefined for sticky and for formData.stickie db.run("INSERT INTO stickies (data) VALUES (?)", [ test ]); });
所以总结一下,我试图将$ scope.formDatavariables传递给我的server.js文件,以便它可以在我的app.post函数中使用(并插入到数据库中)
编辑:按照下面给出的答案更新代码:当我提交表单时,当前得到`ReferenceError:formData不定义。
当你使用ng-model
它将使用某个名字将你的表单组件绑定到作用域。 这意味着
<input ng-model="stickie_text" type="text" id="sticky_content" />
将带给你一个$scope.stickie_text
与你的'formCtrl'
控制器中的组件的当前值。 记住它是一个双向绑定:如果你改变了这个variables,你也可以改变表单控件中的值。
让我们重构这个:
<form ng-submit="submit()" ng-controller="formCtrl"> <input ng-model="stickie_text" type="text" id="sticky_content" /> <button type="submit" id="add_sticky" value="add a new stickie!">new sticky</button> </form>
这种forms只有一个字段: stickie_text
。 另一个是一个button。 到目前为止,您的表单有一个字段,它以指定的名称( $scope.stickie_text
) 存储和读取作用域中的数据。
当你提交表单时,你的$scope.submit
函数被调用:
$scope.submit = function() { $http .post('/api/stickies', {what: to, put: here}) .success(function(data){ //what to do here }) .error(function(data){ console.log('Error: ' + data); }); };
所以这是你的处理程序,最大的问题是:
- 在这里做什么?
- 我如何发布数据?
- 我发布后如何处理相同的数据?
注意我改变了你的处理程序 – 放置注释并replace数据对象。
POST数据必须与服务器端相匹配。 因此,如果您的粘贴必须通过networking下的名称req.body.stickie
(通过这种方式,在服务器端存在req.body.stickie
expression式),您必须撰写的数据是: {stickie: $scope.stickie_text}
。 这是因为stickie_text
是您在视图中使用的名称,所以它是var这个字段将读取和写入的范围的名称。 请记住在应用程序中安装body parser
中间件。
我不记得很多的nodejs,但是如果我是对的,也许这样做:
app.post('/api/stickies',function(req,res){ var test = req.body.stickie; db.run("INSERT INTO stickies (data) VALUES (?)", [ test ]); });
只要您使用客户端的相同数据密钥,就可以做到这一点。 你也应该在响应( res
)对象中写一些东西,但这取决于你和nodejs,以及你喜欢的方式(比如res.write
)。
所以,我们将坚持同样的例子:
$scope.submit = function() { $http .post('/api/stickies', {stickie: $scope.stickie_text}) .success(function(data){ //what to do here? it's up to you and the data you write from server. }) .error(function(data){ console.log('Error: ' + data); }); };
testing这个,并检查你的数据库。
编辑 – @ koushabuild议 – 为表单数据使用事实上的命名空间(就像你之前说的formData
– 记住这是一个例子,你可以使用任何你想要的名字)。 它是这样工作的:
-
为
ng-model
中的每个字段提供前缀,因此同一个对象包含表单数据。 既然你只有一个领域,那就足够了:ng-model="formData.stickie"
故意我改变了variables名称以匹配在nodejs服务器中等待的名字。 不要忘记这一点,因为我将直接将对象用作数据。
-
$scope.formData
的最终值将是{stickie: whatEverIsInTheInputField}
。 如果我使用ng-model="formDta.foo"
作为绑定添加了另一个字段,则$scope.formData
的最终值将是{stickie: whatEverIsInTheInputField, foo: otherFieldValue}
。 -
我直接在http请求中使用
$scope.formData
:$scope.submit = function() { $http .post('/api/stickies', $scope.formData) .success(function(data){ //what to do here? it's up to you and the data you write from server. }) .error(function(data){ console.log('Error: ' + data); }); };
-
虽然
formData
事先并不存在于$scope
中,但是在它下面创buildformData
之前,绑定会创build它。