AngularJS error: TypeError: v2.login is not a function

Nick picture Nick · Aug 14, 2015 · Viewed 39.3k times · Source

I would like to call the login function when I click the login button but keep getting the error message in the title. Can someone point out the error in my script?

login.js code below:

And the code for login.html is also below:

<div class="container" style="max-width: 300px">
    <form class="form-signin">       
      <h2 class="form-signin-heading" style="text-align: center">Please Sign In</h2>
      <input type="text" class="form-control" name="username" ng-model = "username" placeholder="Email Address" required="" autofocus="" />
        </br>
      <input type="password" class="form-control" name="password" ng-model = "password" placeholder="Password" required=""/>
        </br>
      <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="login()">Login</button>   
    </form>
  </div>

Answer

Aaron Gray picture Aaron Gray · Sep 30, 2015

Edge case here, but I want to mention it for posterities' sake. I got this same error when using the controllerAs pattern with a form name with the same value as ng-submit. For example:

<form name="authCtrl.signUp" ng-submit="authCtrl.signUp()">

Throws: TypeError: v2.signUp is not a function

The solution was to change the name of the form to something different:

<form name="authCtrl.signUpForm" ng-submit="authCtrl.signUp()">