1

I'm new to angular, having previously worked mostly with jQuery. I am trying to set up an http.post to an ASP.NET MVC application, intercepting a submit button within a form.

To do that, however, I need to access the anti-forgery token that gets generated within the form via @Html.AntiForgeryToken(). That's turning out to be a real pain, because the element created by @Html.AntiForgeryToken() doesn't have an id. Instead, it just has a name.

I cannot figure out how to find an element by name within angular. Note that I'm running "stock" angular, so I only have jQuery lite available.

3
  • Could you wrap the token inside of a div that you give an ID to? Then use angular to grab the div by ID and get its child. Commented May 8, 2015 at 16:51
  • why you don't use plain html <form> tag? Commented May 8, 2015 at 16:51
  • Good idea about wrapping with a <div>, although that's a little messy. As to the form tag, I am using it. But I want to intercept the submit action so that if the connection to the host has dropped the data will be stored locally for later submission when the connection comes back. Commented May 8, 2015 at 16:56

1 Answer 1

1

Turns out it was relatively easy to do, but it doesn't involve an angular construct, which is why I couldn't find it over the last few hours of googling :(.

It's just plain vanilla javascript:

var form = document.forms[0];
var afElem = form.querySelector("input[name='@AntiForgeryConfig.CookieName'");
Sign up to request clarification or add additional context in comments.

1 Comment

Well sure. If you want the EASY way. :P Not knowing what you were trying to do with it (and not having much Angular experience), I was trying to keep it in that realm. I'm glad you got it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.