1. 程式人生 > >angularjs 的模型無法繫結到隱藏域(input hidden)

angularjs 的模型無法繫結到隱藏域(input hidden)

描述一下問題:

在操作表單中的隱藏域的時候發現angularjs的模型無法繫結,比如:

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

這種通常情況下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:

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