AngularJS 不发送隐藏字段值 [英] AngularJS does not send hidden field value

查看:23
本文介绍了AngularJS 不发送隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于特定用例,我必须以旧方式"提交单个表单.意思是,我使用带有 action="" 的表单.响应是流式传输的,所以我不会重新加载页面.我完全知道典型的 AngularJS 应用程序不会以这种方式提交表单,但到目前为止我别无选择.

For a specific use case I have to submit a single form the "old way". Means, I use a form with action="". The response is streamed, so I am not reloading the page. I am completely aware that a typical AngularJS app would not submit a form that way, but so far I have no other choice.

也就是说,我试图从 Angular 填充一些隐藏的字段:

That said, i tried to populate some hidden fields from Angular:

<input type="hidden" name="someData" ng-model="data" /> {{data}}

请注意,显示的是数据中的正确值.

Please note, the correct value in data is shown.

表单看起来像标准表单:

The form looks like a standard form:

<form id="aaa" name="aaa" action="/reports/aaa.html" method="post">
...
<input type="submit" value="Export" />
</form>

如果我点击提交,则不会向服务器发送任何值.如果我将输入字段更改为输入文本",它会按预期工作.我的假设是隐藏字段并没有真正填充,而文本字段实际上是由于双向绑定而显示的.

If I hit submit, no value is sent to the server. If I change the input field to type "text" it works as expected. My assumption is the hidden field is not really populated, while the text field actually is shown due two-way-binding.

有什么想法可以提交由 AngularJS 填充的隐藏字段吗?

Any ideas how I can submit a hidden field populated by AngularJS?

推荐答案

不能对隐藏字段使用双重绑定.解决方案是使用括号:

You cannot use double binding with hidden field. The solution is to use brackets :

<input type="hidden" name="someData" value="{{data}}" /> {{data}}

在 github 上查看此线程:https://github.com/angular/angular.js/pull/2574

EDIT : See this thread on github : https://github.com/angular/angular.js/pull/2574

从 Angular 1.2 开始,您可以使用 'ng-value' 指令将表达式绑定到输入的 value 属性.此指令应与输入单选框或复选框一起使用,但适用于隐藏输入.

Since Angular 1.2, you can use 'ng-value' directive to bind an expression to the value attribute of input. This directive should be used with input radio or checkbox but works well with hidden input.

这是使用 ng-value 的解决方案:

Here is the solution using ng-value:

<input type="hidden" name="someData" ng-value="data" />

这是一个使用带有隐藏输入的 ng-value 的小提琴:http://jsfiddle.net/6SD9N

Here is a fiddle using ng-value with an hidden input: http://jsfiddle.net/6SD9N

这篇关于AngularJS 不发送隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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