一尘不染

为什么我的Angular控制器未定义?

angularjs

我一直在跟随一个教程,教科书向我保证这可以工作,但是

错误:[ng:areq]参数’SimpleController’不是一个函数,未定义

为什么?我掉毛了,在它上面上下移动,看不到问题。为什么SimpleController出现未定义状态?

<html ng-app>
<head>
    <title>
    </title>
</head>
<body>
    <input type="text" ng-model="blah" />

    <div ng-controller="SimpleController">
        <h3>looping a data set</h3>
        <ul>
            <li ng-repeat="cust in customers | filter:blah | orderBy:'city'">
                {{cust.name | uppercase}} - {{cust.city | lowercase}}
            </li>
        </ul>
    </div>

    <script src="angular.js"></script>
    <script>
        function SimpleController($scope)
        {
            $scope.customers = [
                {name: 'John Smith', city: 'Phoenix'},
                {name: 'Jane Smith', city: 'Pittsburgh'},
                {name: 'John Doe', city: 'New York'},
                {name: 'Jane Doe', city: 'Los Angeles'}
            ];
        }
    </script>
</body>
</html>

阅读 262

收藏
2020-07-04

共1个答案

一尘不染

我的猜测是您使用的angular.js版本是1.3.0-beta.15更新的。

有一个重大变化,1.3.0-beta.15即: 默认情况下
angular将不再寻找控制器window。有关更多详细信息,请参见3f2232b5

With the exception of simple demos, it is not helpful to use globals
for controller constructors. This adds a new method to `$controllerProvider`
to re-enable the old behavior, but disables this feature by default.

BREAKING CHANGE:
`$controller` will no longer look for controllers on `window`.
The old behavior of looking on `window` for controllers was originally intended
for use in examples, demos, and toy apps. We found that allowing global controller
functions encouraged poor practices, so we resolved to disable this behavior by
default.

To migrate, register your controllers with modules rather than exposing them
as globals:

因此, 您的代码应该可以像教科书一样正常工作,以确保angular.js版本早于 1.3.0-beta.15

2020-07-04