在我的Angular应用程序中,我有一个绑定到模型的表单。表单没有用于所有模型属性的字段。当用户点击提交按钮时,我想以传统的方式发布表单,而不是AJAX请求(我必须处理已经存在的服务器端脚本)。
如果我在表单中添加一个action
,这基本上是有效的。但很明显,它只按原样提交带有当前字段的表单,而不是像使用$http
服务时那样提交整个模型。此外,它不创建名称属性。
我如何才能用完整的模型数据以老派的方式提交表格?我必须自己创建缺失的表单字段吗?或者有没有办法让Angular为我做这件事?
<script>
angular.module('foobar', []).controller('ContentCtrl', ['$scope', function($scope) {
$scope.content = {
'title': 'Foo',
'subtitle': 'Bar',
'text': 'desc' // this field is missing in the form itself and therefore is not submitted
};
}]);
</script>
<form action="http://postcatcher.in/catchers/521c6510429f840200000169" method="post" ng-controller="ContentCtrl">
<input type="text" name="est" ng-model="content.title" value="asdf">
<input type="text" ng-model="content.subtitle">
<button>submit</button>
<a href="http://postcatcher.in/catchers/521c6510429f840200000169">see post result</a>
</form>
下面是一个plunker:http://plnkr.co/edit/5XYGH9hTe7cpchSFWbTK
Angular在定义了动作的表单时不会做任何特殊的事情。它是纯html表单提交,具有定义的字段。
通常你会使用:
<input type="hidden" ng-model="content.desc">
但angular当前(1.2.0rc1)仍然不支持ng模型绑定到隐藏输入。
解决方法是:
<input type="text" name="text" ng-model="content.text" ng-hide="true">