How to get the hidden input's value by using angularjs?

user3373877 picture user3373877 · Mar 3, 2014 · Viewed 22k times · Source

In angularjs,I want to get the value of the hidden input. such as the following:

<pre>
<input type="hidden" name="captId" value="AqXpRshs9QHfxUbOWqMT" ng-model="captId">
</pre>

How to get the "hidden input" value "AqXpRshs9QHfxUbOWqMT" by using angularjs,not ajax or jquery.

Answer

ug_ picture ug_ · Mar 3, 2014

You can use ng-init to initialize the value so it binds that to your model variable upon creation.

<input type="hidden" name="captId" ng-init="captId='AqXpRshs9QHfxUbOWqMT'" ng-model="captId">

Or you can get it directly if all else fails:

angular.element(document.getElementsByName('captId')[0]).val();
// or dont use angular at all
document.getElementsByName('captId')[0].value

Documentation for angular.element