AngularJS Uncaught ReferenceError:未从模块定义控制器

Jos*_*ard 21 angularjs angularjs-routing

我有以下代码;

var app =
    angular.
        module("myApp",[]).
        config(function($routeProvider, $locationProvider) {
            $routeProvider.when('/someplace', {
                templateUrl: 'sometemplate.html',
                controller: SomeControl
             });
             // configure html5 to get links working on jsfiddle
             $locationProvider.html5Mode(true);
        });

app.controller('SomeControl', ...);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Uncaught ReferenceError: SomeControl is not defined from myApp
Run Code Online (Sandbox Code Playgroud)

问题只是我不能使用app.controller('SomeControl',...)语法?什么时候使用$ routeProvider?是唯一有效的语法:

function SomeControl(...)
Run Code Online (Sandbox Code Playgroud)

Foo*_*o L 42

使用引号:

            controller: 'SomeControl'
Run Code Online (Sandbox Code Playgroud)

  • DUH,谢谢你找到我的小错误;) (2认同)

Mic*_*kin 8

就像Foo L说的那样,你需要加上引号SomeControl.如果不使用引号,则指的SomeControl是未定义的变量,因为您没有使用命名函数来表示控制器.

当您使用您提到的替代方法时,您function SomeControl(...)可以定义该命名函数.否则,Angular需要知道它需要在myApp模块中查找控制器.

使用app.controller('SomeControl', ...)语法更好,因为它不会污染全局命名空间.