使用$ routeParams时,angular.js模块化控制器"未定义"

use*_*779 7 angularjs

以下命令在控制台中返回错误"ReferenceError:未定义ThingCtrl"

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'
    }
];

}]);
Run Code Online (Sandbox Code Playgroud)

但是当控制器被定义为:

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

为什么使用模块化语法不起作用?

jnc*_*ton 11

我相信这个问题在这里:

when('/things', {templateUrl: 'partial.html', controller: ThingCtrl})
Run Code Online (Sandbox Code Playgroud)

这告诉Angular指向ThingCtrl对象,该对象未定义并导致错误.

尝试将控制器名称括在引号中,如下所示:

when('/things', {templateUrl: 'partial.html', controller: 'ThingCtrl'})
Run Code Online (Sandbox Code Playgroud)

这应该允许Angular正确使用依赖注入.