user4266661
user4266661

Reputation:

Angular MVC4: Get Element by Name

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.

Upvotes: 1

Views: 66

Answers (1)

user4266661
user4266661

Reputation:

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'");

Upvotes: 1

Related Questions