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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With