Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Basics (1.x) Controllers and Scope Creating a Controller

Nina Kozlova
Nina Kozlova
11,602 Points

I get an error saying that "mainCtrl" is not a function and thus is undefined.

I've gone through the code with a fine-tooth comb, it's identical to what is on the screen. I mean, identical, every comma, every brace. So why is it not working?

Steven Parker
Steven Parker
231,269 Points

If you post your code, or make a snapshot of your workspace and post the link, someone could look and perhaps identify and explain the issue.

Joel Kraft
Joel Kraft
Treehouse Guest Teacher

Did you find an answer to your problem, Nina? Let us know, and be sure to include your code, as Steven mentioned, if you need more help. Thanks!

Change .controller to .controller('mainCtrl',['$scope',function($scope) { $scope.helloWorld = function() { console.log('This is the helloWorld controller function'); }; }]);

I had the same error: angular.js:12416 Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined

Here is the code:

angular.module("todoListApp", [])
.controller('mainCtrl', function($scope){
  $scope.helloWorld = function() {
    console.log("Hello there! this is the controller function.");
  };
});
Joel Kraft
Joel Kraft
Treehouse Guest Teacher

That code works for me, stevenorton. Perhaps if you share the contents of your HTML file, I could troubleshoot further. Also, are you in workspaces? Or are you working on your local machine?

1 Answer

Aida Pandur
Aida Pandur
828 Points

It is solved for me by changing the name of the controller same with the name of the js file. when I named the controller "app"