2

Does someone know how to do this simple thing : get a value from a hidden input field with angularjs ? Googled a lot without real success... C# in server side writes a product id, and I need to retrieve it to pass it to my angularjs json service.

Thanx a million times if you're able to answer!

joe cool
  • 315
  • 7
  • 14

4 Answers4

3

This is how I do it:

@functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + ":" + formToken;
        }
    }

<input id="antiForgeryToken" type="hidden" value="@GetAntiForgeryToken()" />

var antiForgeryToken = $("#antiForgeryToken").val();

EDIT

Without jQuery:

var antiForgeryToken = document.getElementById("antiForgeryToken").value;

Using the angular $document:

var antiForgeryToken = $document.find("#antiForgeryToken").val();
Rob J
  • 6,609
  • 5
  • 28
  • 29
1

@Rob Jacobs has the right approach. Essentially this:

$scope.myHiddenElement = $("#ctl00_cphMainContent_hConsentDisagree").val();

Where your hidden element is:

<asp:HiddenField ID="hConsentDisagree" runat="server">

Or...

<input type='hidden' id='ctl00_cphMainContent_hConsentDisagree' value='whatever' />

EDIT To Avoid breaking the spirit of AngularJs you could change the ASP.NET piece to something like this:

var hiddenField = new HtmlInputHidden();
hiddenField.Value = "myValue";
hiddenField.Attributes["ng-model"] = "my-hidden-field";

then it's just this in the controller:

$scope.my-hidden-field

EDIT Number 2

var hiddenField = new HtmlInputHidden {Value = "myValue", ID = "hiddenfield"};
hiddenField.Attributes.Add("ng-model","myhiddenfield");
myDiv.Controls.Add(hiddenField);

Does not seem to work... However this does:

$scope.myTest = $('#MainContent_hiddenfield').val();

Yeah... I know, it breaks the spirit of Angular but... sometimes you gotta just make it work. I don't have a better answer.

Rod Hartzell
  • 420
  • 3
  • 9
  • ok but the purpose of angularjs is to avoid jquery : http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background – joe cool Dec 16 '13 at 22:02
  • I have found that jQuery is not completely indispensable. I edited my answer with another approach that should work. – Rod Hartzell Dec 16 '13 at 22:33
  • See EDIT Number 2... This was done with the built in jQuery lite that comes with AngularJs. No external references to jQuery were used. So... While it is still jQuery-esque, it is really Angular. – Rod Hartzell Dec 17 '13 at 16:04
  • Yes, I know it's the built-in angular jqlite, but a lot of questions remains unresolved about input type hidden and the way to access it by ngModel way... Thanx. – joe cool Dec 17 '13 at 16:49
1

A small enhancement to @Rod Hartzell solutions

var hiddenField = new HtmlInputHidden {Value = "myValue", ID = "hiddenfield" ClientIDMode = ClientIDMode.Static};
hiddenField.Attributes.Add("ng-model","myhiddenfield");
myDiv.Controls.Add(hiddenField);

and

$scope.myTest = $('#hiddenfield').val();

I've set the ClientIdMode to Static (https://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.110%29.aspx). This way the ClientID value is set to the value of the ID property.

Hope that helps.

Avinash Gadiraju
  • 723
  • 1
  • 7
  • 16
0

You can try generating ng-model="hiddenValue" attribute on the field from server side and then try to get the value in your controller with $scope.hiddenValue. Of course the hidden field should be in the the scope of the controller.

shizik
  • 910
  • 6
  • 16