angularjs 的模型无法绑定到隐藏域(input hidden)

描述一下问题:

在操作表单中的隐藏域的时候发现angularjs的模型无法绑定,比如:

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

在网上找到了解决办法,特意记录;原文:http://stackoverflow.com/questions/18446359/angularjs-does-not-send-hidden-field-value

这种通常情况下data一直无法取到值;

有以下几种解决办法:

<input type="text" name="someData" ng-model="data" style="display: none;"/>

<input type="hidden" required ng-model="data.userid" ng-init="data.userid=pivot.id" />

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

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

最优回答如下:

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

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

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

EDIT:

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.

Here is the solution using ng-value:


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值