为什么我的 Angular 控制器未定义? [英] Why is my Angular controller undefined?

查看:27
本文介绍了为什么我的 Angular 控制器未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习教程,教科书向我保证这是可行的,但它正在轰炸

<块引用>

错误:[ng:areq] 参数SimpleController"不是函数,得到未定义

为什么?我已经给它整理了绒毛,上下检查过,但我看不出有什么问题.为什么 SimpleController 未定义?

<头><标题><身体><input type="text" ng-model="blah"/><div ng-controller="SimpleController"><h3>循环数据集</h3><ul><li ng-repeat="客户定制 | filter:blah | orderBy:'city'">{{客户名称 |大写}} - {{cust.city |小写}}

<script src="angular.js"></script><脚本>函数 SimpleController($scope){$scope.customers = [{名称:'约翰史密斯',城市:'凤凰城'},{名称:'简·史密斯',城市:'匹兹堡'},{名称:'John Doe',城市:'纽约'},{名称:'Jane Doe',城市:'洛杉矶'}];}</html>

解决方案

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

1.3.0-beta.15 有一个重大变化,因此:angular 将不再默认在 window 上寻找控制器.请参阅 3f2232b5 了解更多详情:

除了简单的demo,使用globals是没有帮助的对于控制器构造函数.这为 `$controllerProvider` 添加了一个新方法重新启用旧行为,但默认禁用此功能.突破性变化:`$controller` 将不再在 `window` 上寻找控制器.查看控制器的窗口"的旧行为最初是打算用于示例、演示和玩具应用程序.我们发现允许全局控制器函数鼓励不良做法,因此我们决定通过以下方式禁用此行为默认.要迁移,请使用模块注册您的控制器而不是公开它们作为全局变量:

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

I've been following a tutorial, the textbook assures me that this works, but it's bombing with

Error: [ng:areq] Argument 'SimpleController' is not a function, got undefined

Why? I have linted it, been up and down it and I cannot see a problem. Why is SimpleController coming up undefined?

<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>

解决方案

My guess is that the angular.js version you are using is 1.3.0-beta.15 or newer.

There is a breaking change in 1.3.0-beta.15 so that: angular will no longer look for controllers on window by default. See 3f2232b5 fore more detail:

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:

Therefore, your code should be working fine like the textbook assures for angular.js version older than 1.3.0-beta.15.

这篇关于为什么我的 Angular 控制器未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆