kauffmanes
kauffmanes

Reputation: 518

AngularJS UI-Router won't recognize a defined controller on the module

I'm in the process of learning Angular, and I'm trying to set up a state with a controller using ui-router. I have a main module named app.js, and then sub-modules based on different content.

The first sub-module is diary.js. Everything is working with this controller other than the controllers. The state works in the UI, etc. When I make the controller directly in diary.js (such as controller : function () { //stuff }, it works fine). But when I try to include an already defined controller for the diary state (like it's currently written), I get the following error (I couldn't post the whole error because stackoverflow won't let me have that many links):

"Error: [ng:areq] errors.angularjs.org/1.2.23/ng/areq?p0=DiaryCtrl&p1=not%20aNaNunction%2C%20got%20undefined at Error (native) ...

/** diary.js */
'use strict';

(function () {

angular.module('diary', ['ui.router'])  

    .config(['$stateProvider', function ($stateProvider) {

        // States
        $stateProvider.state('diary', {
            url         : '/diary',
            templateUrl : 'diary/html/diary.html',
            controller  : 'DiaryCtrl'
        });
    }]);

}).call();

Here is the code for the DiaryCtrl.js (the defined controller).

/** DiaryCtrl.js */
'use strict';

angular.module('diary')

    .controller('DiaryCtrl', [$scope, $http, function ($scope, $http) {

        $http.get('api/json/diaries.html').success(function (data) {
            $scope.diaries = data;
        }).error(function (status) {
            console.log(status);
        });

    }]);

I'd appreciate any help. Let me know if you need more information.

Upvotes: 1

Views: 2118

Answers (1)

jnthnjns
jnthnjns

Reputation: 8925

I am fairly certain it is because your injections ($scope & $http) in the DiaryCtrl are not strings:

.controller('DiaryCtrl', [$scope, $http, function ($scope, $http)

should be:

// Notice the added quotations around the scope and http injections in the array
.controller('DiaryCtrl', ['$scope', '$http', function ($scope, $http) {

Upvotes: 2

Related Questions