javascript - 即使 Controller 可用,Angular JS 也会抛出错误

标签 javascript angularjs single-page-application

我是 Angular JS 的新手,我正在尝试一些示例...但是当我尝试加载我的应用程序时出现了一个非常奇怪的异常...

这是我的 Angular JS 代码:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
        <script src="JS/Controllers.js"></script>
    <body ng-app="myapp">
            <div ng-controller="HelloController">
                <h2>Hello {{helloTo.title}} !</h2>
            </div>
            <div ng-controller="MyController" >
                <span ng-show="myData.showIt"></span>
                <span ng-hide="myData.showIt"></span>
            </div>
        </body>

这是我的 Controller 代码:

angular.module("myapp", []).controller("HelloController", function($scope) {
    $scope.helloTo = {};
    $scope.helloTo.title = "Test";
} );

angular.module("myapp1", []).controller("MyController", function($scope) {
  $scope.myData = {};
  $scope.myData.showIt = true;
});

Firebug 记录的错误: 错误:[ng:areq] http://errors.angularjs.org/1.2.5/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js 第83行

我正在使用 angular js 版本 1.2.5...

最佳答案

您正在创建两个不同的 Angular 模块,每个模块有一个 Controller ,而不是一个模块有两个不同的 Controller ,这就是您想做的,只需更改您的代码以匹配此语法:

angular.module("myapp", [])
    .controller("HelloController", function($scope) {
        // ...
    })
    .controller("MyController", function($scope) {
        // ...
    });

或者这个:

var myApp = angular.module("myapp", []);

myApp.controller("HelloController", function($scope) {
    // ...
});

myApp.controller("MyController", function($scope) {
    // ...
});

关于javascript - 即使 Controller 可用,Angular JS 也会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753048/

相关文章:

javascript - 我可以用一个 HTML5 应用程序控制两个浏览器窗口吗?

angularjs - 在不违反 RESTful 原则的情况下,在 Angular 中进行身份验证和授权的最佳实践?

angularjs - 当迭代对象集合时,有什么方法可以动态分配 ng-controller 吗?

javascript - AngularJs:通过单击该文本将文本设置为文本框

angularjs - AngularJS 应用程序中的 Svg ClipPath 与 HTML 基本元素

javascript - requirejs如何加载commonjs包?

php - 如何在 Laravel 5 中禁用 cookie?

javascript - 包含带有phonegap的指令

javascript - 使用 if 语句使 div 淡入淡出

javascript - 如何查找数组中所有出现的元素的索引(Ramda.js 方式)?