Binding to Hidden Fields does not seem to work

8,691 views
Skip to first unread message

avina...@gmail.com

unread,
Sep 7, 2012, 4:32:27 AM9/7/12
to ang...@googlegroups.com
Hello, 

Binding to Hidden Fields does not seem to work. 


clicking "Test Hidden" button brings up content of the hidden field. 

Changing this from : 
<input type "hidden" name 'aHiddenField' ng-model "forHiddenBinding"/> 

to this : 
<input type "text" ng-show = "false" name 'aHiddenField' ng-model "forHiddenBinding"/> 
gets it working. 

Although, I can get away with the work around; it will be nice to see the hidden field working. 

Or am I doing something wrong ?

Thanks, 
Avinash

Peter Bacon Darwin

unread,
Sep 7, 2012, 5:37:38 AM9/7/12
to ang...@googlegroups.com
The question is what do you need the hidden field for?  In angular you should probably be using a model to store hidden data.
Pete

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

avina...@gmail.com

unread,
Sep 7, 2012, 7:07:46 AM9/7/12
to ang...@googlegroups.com
Hello Pete, 

I'm building what you may call a 'vanilla web app' with ASP MVC2. Typically, such apps use JQuery to do all of the client side stuff. 

I find using NG much cleaner, simpler and achieve similar or better results with fewer lines of code. 

But I don't want to throw away all of the server side goodness and so I'm trying out a mix and match approach. 

Case in point, the hidden is populated based on some code that executes on the client and the server on post back maps it to a view model and then the view model is used to make decisions. 

Thanks, 
Avinash

Peter Bacon Darwin

unread,
Sep 7, 2012, 4:54:30 PM9/7/12
to ang...@googlegroups.com
Looks like this line of code is causing you the problem:  https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L366.
Angular just ignores hidden input elements.
If the hidden field was constant and set and read only by the server life would be easy. Job done.
Since you need to access and modify this in your AngularJS app then you could do something nasty using document.forms[] like you do in the alert - probably inside a controller.
The best option would be to write your own input directive that looks for type=hidden and in that case wires up the ng-model stuff.
Pete

ha...@familieallis.nl

unread,
Oct 2, 2012, 10:25:46 AM10/2/12
to ang...@googlegroups.com, avina...@gmail.com
The easiest solution is to just use normal binding inside the value attribute. Changed your fiddle to look like this: http://jsfiddle.net/qsFWX/3/

Rob Chandler

unread,
Apr 5, 2013, 10:29:01 AM4/5/13
to ang...@googlegroups.com, avina...@gmail.com, ha...@familieallis.nl
This is a solution which worked until I found a problem.

When a user hits the back button the back button, the value attribute is populated with the value that was present when leaving the page, and not the angular binding, so it is lost. Not sure if this is a problem for the OP but it is for my use case.

Jason Lines

unread,
Sep 10, 2013, 10:57:55 PM9/10/13
to ang...@googlegroups.com
Is there a reason for this? It seems inconsistent with the rest of the input types, I would expect it to just act like a text field that is rendered offscreen (which is also a workaround for this issue).
Reply all
Reply to author
Forward
0 new messages