我是 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/