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一直无法取到值;
有以下几种解决办法:
1,
<input type="text" name="someData" ng-model="data" style="display: none;"/>
2,
<input type="hidden" required ng-model="data.userid" ng-init="data.userid=pivot.id" />
3,
<input type="hidden" name="someData" value="{{data}}" />
4,
<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:
<input type="hidden" name="someData" ng-value="data" />
Here is a fiddle using ng-value with an hidden input: http://jsfiddle.net/6SD9N