Tomáš Fejfar
Tomáš Fejfar

Reputation: 11217

Hidden fields in AngularJs

How do I access hidden fields in angular? I have an app, where I want to submit a form for each of items in the list. The form is simple - it has submit button and a hidden field holding the ID value. But it does not work. The value is empty.

I updated the default angular example to display the situation - the todo text is in hidden field.

http://jsfiddle.net/tomasfejfar/yFrze/

Upvotes: 16

Views: 54506

Answers (7)

Maximilian Eberl
Maximilian Eberl

Reputation: 85

I have to correct (improve) myself:
You can do it more elegantly:

<form>
<input type="text" ng-model="formData.foo" />
<input type="hidden" id="bar" value="YourHiddenValue" />
<button ng-click="processForm()"> SUBMIT </button>
</form>

and then in the JavaScript controller:

$scope.formData.bar = "";
try {$scope.formData.bar = document.getElementById("bar").value;} 
catch(e) {alert(e.message);}
alert("foo="+$scope.formData.foo+ " bar="+$scope.formData.bar);

So you can have as many hidden fields as you like.

Upvotes: 0

neela kanta
neela kanta

Reputation: 11

Use ng-binding="{{employee.data}}". It will work properly.

Upvotes: 0

phteven
phteven

Reputation: 1383

If you want to pass the ID from the ng-repeat to your code, you don't have to use a hidden field. Here's what I did:

For example, let's say I'm looping through a collection of movies, and when you click the "read more" link it will pass your ID to your JS code:

<ul>
  <li ng-repeat="movie in movies">
    {{movie.id}} {{movie.title}} <a href="#" ng-click="movieDetails(movie)">read more</a>
  </li>
</ul>

Then in your JS code, you can get the ID like this:

$scope.movieDetails = function (movie) {
  var movieID = movie.id;
}

Upvotes: 8

Arraxas
Arraxas

Reputation: 421

Simpler:

<input type="hidden" name="livraisonID" value="{{livraison.id}}"/>

It works!

Upvotes: 1

pocesar
pocesar

Reputation: 7050

If you don't want to hardcode anything in your javascript file, you can either load it via AJAX, or do:

<input type="hidden" name="value" ng-init="model.value=1" value="1">

this way, you can keep the form functionality with JS off, and still use the hidden field in AngularJS

Upvotes: 13

Maximilian Eberl
Maximilian Eberl

Reputation: 85

You can do something like this.
It is a dirty trick, but it works (like most dirty tricks ;-)
You just use the form name as Your hidden field
and always give the form the id "form"

<!doctype html><html ng-app><head>
<script src="angular-1.0.1.min.js"></script>
<script>
function FormController($scope) {
  $scope.processForm = function() {alert("processForm() called.");
    $scope.formData.bar = "";
    try {$scope.formData.bar = document.getElementById("form").name;} 
    catch(e) {alert(e.message);}
    alert("foo="+$scope.formData.foo+ " bar="+$scope.formData.bar);
  };
}
</script></head><body>
<div ng-controller="FormController">
<form name="YourHiddenValueHere" id="form">
<input type="text" ng-model="formData.foo" />
<button ng-click="processForm()"> SUBMIT </button>
</form>
</div></body></html>

This allows You to use ONE Controller for ALL forms and send
them to ONE server script. The script than distinguishes by the
form name (formData.foo) and knows what to do.
The hidden field names the operation in this scenario.

Voila - You have a complete application with as
many forms You want and one server script
and one FormController for all of them.

Upvotes: 2

Andrew Joslin
Andrew Joslin

Reputation: 43023

In your simpler fiddle, the problem can be fixed by using ng-init or setting an initial value in the controller. The value attribute won't effect the ng-model.

http://jsfiddle.net/andytjoslin/DkMyP/2/

Also, your initial example (http://jsfiddle.net/tomasfejfar/yFrze/) works for me in its current state on Chrome 15/Windows 7.

Upvotes: 2

Related Questions