0

The code can be viewed at http://ec2-52-1-22-32.compute-1.amazonaws.com/

In the login form, I call the currently simple login() function of the AuthController from ng-click on the button of the the login form. When I call this function, I get an error in the console that I cannot decipher. In FF, I get an "args is null" error. In Chrome, errors says "TypeError: Cannot read property '0' of null". Not sure how to debug this? Why doesn't it work?

PS: You can enter any username or password.

3
  • As someone who has been 6 years on SO and with 6k+ rep, you should know that a question should be self-containing without resorting to external links Commented Feb 16, 2015 at 23:11
  • As someone who's been coding for much more than six years, I have a feeling the error requires more than just posting the HTML fragment and the controller snippet. Commented Feb 17, 2015 at 2:37
  • I apologize for providing more code than usual. I fear that I may cut out the error source... Commented Feb 17, 2015 at 2:51

2 Answers 2

5

The login in ng-click="login()" refers to the "login" form, rather that to the $scope.login function.

This is what you have:

<div ng-controller="AuthController as auth">  
   <form name="login">
     <button ng-click="login()">login</button>
   </form>
</div>

And in the controller you have:

$scope.login = function(){
}

The form's login variable published on the scope hides the login function defined in the controller. In other words, Angular fails as it is not invoking a function for ng-click="login()".

Ways to fix:

#1 use ControllerAs syntax:

this.login = function(){
}

and

<button ng-click="auth.login()">

Or, #2 - rename the function or the form:

<form name="loginForm">
   <button ng-click="login()">login</button>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed a problem I was having. I had added a form element for validation and named it the same as my click method on the button to submit. Nice, simple explanation also.
0

In AuthService you have a '/' instead of '.' - that's starting a regex pattern and probably messing up the rest of your app:

login: function(data) {
  return $http/post('/api/auth/login')
},

Some more issues that may be causing it. You're using the "controller as" in your html, but the controller is still using $scope:

ng-controller="AuthController as auth"

You're also still referencing objects/functions as if they were on $scope...for example ng-click="login()" should be ng-click="auth.login()".

1 Comment

Good catch! But, unfortunately, it didn't change the fact that I get that same error.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.