我如何提交表单到服务器上的控制器? [英] How do I submit a form to a controller on the server?

查看:212
本文介绍了我如何提交表单到服务器上的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度,想知道如何把表单提交给服务器一旦数据被填充。

I am new to Angular and would like to know how to actually submit a form to the server once the data is filled in.

在angularjs.org菜谱的形式实例只保存在客户端的状态。我如何提交到服务器?

The cookbook form examples on angularjs.org only save the state on the client. How do I submit to the server?

另外,我怎么使用jQuery的 form.submit()在表格上NG:点击=保存()功能?

Alternatively, how do I use jQuery's form.submit() on the form in the ng:click="save()" function?

编辑 - 找到2的方式来做到这一点(我也删除HTML标记之前,我贴 - 只是指的是高级形式食谱示例源)

Edit - Found 2 ways to do this ( I also removed the html markup i pasted before - just refer to the advanced form cookbook example for the source)


  1. <一个href=\"http://webpac2.rot13.org:3000/conference/Work\">http://webpac2.rot13.org:3000/conference/Work (由多布里察Pavlinusic)来使用资源,将数据发送到JSON格式的服务器去角的方式。我曾与该问题在服务器端 - 角被寄往罚款,但Grails的是重整它(根据萤火虫和请求内容长度)。我需要看看这个多。如何更改内容类型的角像$节约资源()的方法?

  1. http://webpac2.rot13.org:3000/conference/Work (by Dobrica Pavlinusic) to go the angular way using a resource to send the data to the server in json format. I had issues with that on the server side - Angular was sending it fine but grails was mangling it (according to firebug and request content-length). I need to look into this more. How do i change the content-type in angular for a resource method like $save() ?

放在一个表单,并使用一个提交按钮。因为我没有做一个页面的Web应用程序,我用这个方法。大多数人验证客户端还增加了一些这足以为我的服务器上。

Put a form in and use a submit button. Since I am not doing a single page web app, I used this method. Most validations were on the client and a few more on the server which were sufficient for me.

只是把这个位置让别人可以使用这个可能的解决方案,以及获得的JavaScript反应/上最好的办法角度专家。

Just putting this here so that someone else can use this for possible solutions as well as to get the reaction of the javascript/angular experts on the best approach.

谢谢
拉杰什

推荐答案

请注意,有查看(你的HTML模板),并严格分离的逻辑(您的JS code) - 主要是因为可测性的

Note, there is strict separation of view (your html template) and logic (your JS code) - mainly because of testability.

正确的做法是只送你的模型到服务器,使用的 $资源(适用于REST)或低级别的 $ HTTP 。而不是在模板做的工作。

The right way is to just send your model to the server, using $resource (for REST) or low level $http. Instead of doing the job in the template.

简单的例子 - HTML模板

Simple example - HTML template

First: <input type="text" ng-model="person.first" />
Last: <input type="text" ng-model="person.last" />
<button ng:click="save()">Save</button>

JavaScript的 - 控制器

JavaScript - controller

function FormCntl($scope, $http) {
  $scope.save = function() {
    $http.put('/save.py', $scope.person);
  };
}

这篇关于我如何提交表单到服务器上的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆