当前位置: 首页 > 面试题库 >

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

萧鸿轩
2023-03-14
问题内容

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

错误:[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>

问题答案:

我的猜测是您使用的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



 类似资料:
  • 问题内容: 我在我的angular js项目中遇到了$ scope的麻烦。例如,当我在输入字段上使用ng-model =“ modelExample”时,我无法使用$ scope.modelExample在我的js中访问它。还有其他人有类似的问题吗? 这很奇怪,调用了一个函数,但是ng-model没有绑定。请参阅下面的代码,当我提交表单时,将调用refreshResults()函数,但$ scop

  • 我想了解一些关于Spring的东西。我正在尝试配置它,但是有些东西我不明白,因为Spring似乎不认识我的控制器。 这是我的web.xml 这就是servlet 控制器:

  • 返回文件的URL。 starfxdemodoc.fxml为: 而StarfXDemodocController.java是这样的: 有谁能帮我一下吗? 编辑/更新: 根据james_d的评论(谢谢你James,显然我已经盯着这个问题太久了……),我修复了控制器中的明显错误,并将SimNameField设置为TextField,而不是Label(并更新了上面的代码块以反映这一点)。我还将异常处理更改

  • 什么是微控制器,为什么需要微控制器? 在阅读Arduino 文档的时候, Arduino是一款便捷灵活、方便上手的开源电子原型平台。包含硬件(各种型号的Arduino板)和软件(ArduinoIDE)。由一个欧洲开发团队于2005年冬季开发。其成员包括Massimo Banzi、David Cuartielles、Tom Igoe、Gianluca Martino、David Mellis和Nic

  • 我有以下html: 我试图获得如下范围: 但我收到:。 我是angularJS初学者,我只是不明白为什么这不起作用。 更新:这也不起作用: 更新2:我试图在控制台中打印出作用域的所有代码:

  • 问题内容: 我试图制作一个延迟X毫秒执行异步函数的函数。 为了演示的目的,以下是异步函数,该函数采用URL: 目的 我在这里的目标是要有一个函数,它将接受X 的参数,然后每隔X ms调用一次,直到不再有其他参数为止。 基本上,我希望每次调用都用X ms分隔。 例如,假设我连续打电话20次。通常,这20个呼叫将立即进行。我想要的是确保20个呼叫之间都存在Xms的延迟。 暂定 解决这个问题的想法是建立