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

itai zilcha
itai zilcha
6,142 Points

Argument 'mainCtrl' is not a function, got undefined

I got this annoying problem which i do not know how to solve. I've tried to do many things but I just unable to fix the issue. This is my JS :

angular.module("todoListApp", []) .controller('mainCtrl', function($scope) { $scope.helloWorld = function() { console.log("hello world"); }; });

index.html

<body ng-app="todoListApp"> <div ng-controller="mainCtrl" class= "list"> <input type= "checkbox"/> <label class="editing-label">A sample to do</label> <input class="editing-label" type="text" /> <div class="actions"> <a href="">edit</a> <a href="" ng-click="helloWorld()">save</a> <a href="" class="delete">delete</a> </div> </div>

<script src="vendor/angular.js" type="text/javascript"></script> <script src="scripts/app.js" type="text/javascript"></script> </body> </html> --> Maybe someon can take a look and find the problem.

Paul Calabrese
Paul Calabrese
13,516 Points

I am having this issue as well. In my console I get an error that says "Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined." The error references the line 12416 in the angular.js file.

This is my actual code:

'''html <!doctype html> <html lang="en"> <head> <title></title> <link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'> <link href='styles/main.css' rel='stylesheet' type="text/css"> </head> <body ng-app="todoListApp"> <h1>My Todo's</h1> <div ng-controller="mainCtrl" class="lists"> <input type="checkbox"/> <label class="editing-label">A sample todo</label> <input class="editing-label" type="text"/> <div class="actions"> <a href="">edit</a> <a href="" ng-click="helloWorld()">save</a> <a href="" class="delete">delete</a> </div> </div> <script src="vendor/angular.js" type="text/javascript"></script> <script src="scripts/app.js" type="text/javascript"></script> </body> </html> '''

2 Answers

Aidan Pine
Aidan Pine
9,210 Points

Do you have todoListApp declared somewhere in your html?

You should have something like:

<body ng-app="todoListApp">
...
</body>

You'll also need to include the controller in your html if you haven't done so.

<div class="actions" ng-controller="mainCtrl">
  <a href="">edit</a>
  <a href="" ng-click="helloWorld()">save</a>
  <a href="" class="delete">delete</a>
</div>
Abhishek koserwal
Abhishek koserwal
2,554 Points

Code works fine ! as described in the video, problem is with Treehouse Workspace. When you launch preview for your app in the browser. View source code of the page and click on the app.js file.

<script src="scripts/app.js" type="text/javascript"></script>

You will see this

angular.module("todoListApp", []);

It is loading code from first workspace not your current code. Try it in your local environment. It will work.

Jose Agrelot
Jose Agrelot
13,416 Points

It worked for me locally...