7

the following returns an error in the console "ReferenceError: ThingCtrl is not defined"

var myApp = angular.module('myApp', []);

myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
    when('/things', {templateUrl: 'partial.html', controller: ThingCtrl}).
    when('/things/:id', {templateUrl: 'detail.html', controller: ThingCtrl}).
otherwise({redirectTo: '/things'});
 }]);

myApp.controller('ThingCtrl', ['$scope', '$routeParams', function($scope, $routeParams) {
$scope.thing = [
    {
        'title':'first thing',
        'first':'one',
        'second': 'two',
        'third': 'three'
    }
];

}]);

however it works fine when the controller is defined like:

function ThingCtrl($scope, $routeParams) {
        $scope.thing = [
    {
        'title':'first thing',
        'first':'one',
        'second': 'two',
        'third': 'three'
    }
  ]
};

Why does it not work using the modular syntax?

2

1 Answer 1

11

I believe that the issue is here:

when('/things', {templateUrl: 'partial.html', controller: ThingCtrl})

This is telling Angular to point at the ThingCtrl object, which is undefined and causes an error.

Try enclosing the controller name in quotes like this:

when('/things', {templateUrl: 'partial.html', controller: 'ThingCtrl'})

This should allow Angular to properly use dependency injection.

Sign up to request clarification or add additional context in comments.

Comments

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.