Phil.Wheeler Phil.Wheeler - 11 months ago 70
AngularJS Question

Angular Controller is not a function

This seems to be yet another variation on a very common theme with Angular. I have a controller that isn't being picked up and I can't pinpoint what's missing.

Here are the key areas:

My Layout.cshtml page has the following:

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src=""></script>

<script src="~/js/utility/utility.js"></script>

<script src=""></script>
<script src=""></script>

<script src="~/js/app.js"></script>

<script src="~/js/utility/config.js"></script>

<!-- models -->
<script src="~/js/models/game.js"></script>

<!-- services -->
<script src="~/js/services/game.js"></script>

<!-- controllers -->
<script src="~/js/controllers/home-controller.js"></script>

App.ts looks like this:

/// <reference path="utility/utility.ts" />

var app;
(function () {
'use strict';

app = angular.module(Utility.Resources.appName, ['ngRoute'])


My config.ts is set up like this (a few deliberate redundancies in the routes to begin with):

(function () {
'use strict';

app.config(function ($routeProvider, $locationProvider) {'routes');
$routeProvider.when(, {
}).when(Utility.Urls.home, {
templateUrl: Utility.Templates.home
}).when(, {
redirectTo: Utility.Urls.home
//templateUrl: Utility.Templates.home

}); ($rootScope, $location) {'running');

$rootScope.$on('$routeChangeStart', function (event, nextRoute, currentRoute) {"Started");

And the controller in question looks like this:

var Controllers;
(function (Controllers) {
'use strict';
var Home = (function () {
function Home($scope, gameService) {
$scope.vm = this;"In Home Controller");

$ = [];
$scope.categories = [];
$scope.selectedGame = {};

gameService = gameService;
$scope.get = function () {"Getting");

Home.$inject = [
Utility.Angular.$scope, Utility.Services.gameService
return Home;
Controllers.Home = Home;
})(Controllers || (Controllers = {}));

So far I can't see anything that's obviously missing but when opening the home template...

<div class="row" ng-controller="Controllers.Home">
<div class="col-md-3">
<ul class="list-group">
<li class="list-group-item list-group-item-info" ng-repeat="game in games" id="{{game.Id}}" ng-click="get(game.Id)">
<p><span class="meta">{{game.Date}}</span> <span class="pull-right"><i class="glyphicon glyphicon-calendar"></i> {{game.Season}}</span></p>
<p>{{game.Team.Name}} vs {{game.Opposition}}</p>

... the debugger returns the following error:
Argument 'Controllers.Home' is not aNaNunction, got undefined

So I'm sure I've overlooked something. Perhaps a dependency in my controller or maybe one of the models? Except I'm not seeing any other errors in the debugger that would indicate a problem.

Any ideas?

georgeawg is basically right. The missing part is in my config:

app.config(function ($routeProvider, $locationProvider) {'routes');
$routeProvider.when(, {
}).when(Utility.Urls.home, {
templateUrl: Utility.Templates.home,
controller: Controllers.Home // <- here

Answer Source

Be sure to declare the controller function to the module as a controller type.

app.controller("Controllers.Home", Controllers.Home);

From the Docs:

Error: ng:areq

Bad Argument

Argument 'Controllers.Home' is not a function, got undefined


AngularJS often asserts that certain values will be present and truthy using a helper function. If the assertion fails, this error is thrown. To fix this problem, make sure that the value the assertion expects is defined and matches the type mentioned in the error.

-- AngularJS Error Reference -- $compile -- ng:areq