我正在重构一些角度代码,我想按类型(控制器文件夹中的控制器等)分隔。
我有两个控制器和一个app.js文件。为什么我得到了,这个未定义的东西不是函数的意思(我在控制台中从两个独立的控制器文件中得到错误)
app.js
var app = angular.module('myApp',['myApp.controllers']);
controllers
app.module('myApp.controllers').controller('DocumentController',function($scope){
$scope.message = "This is the message from Document controller";
})
//
app.module('myApp.controllers').controller('MyController', function ($scope) {
$scope.message = "This is the message from My Controller";
})
HTML
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="lib/jquery-1.9.1.js"></script>
<script src="lib/angular-1.2.js"></script>
<script src="app.js"></script>
<script src="controllers/MyController.js"></script>
<script src="controllers/DocumentController.js"></script>
</head>
<body>
this is from the index.html page
<div ng-controller="MyController">
{{message}}
</div>
<script>
</script>
</body>
</html>
答案 0 :(得分:1)
您需要声明&#39; myApp.controllers&#39;模块才可以使用它。要声明它,您需要将一个依赖项数组传递给angular.module。尝试将app.js更改为:
app.js
var app = angular.module('myApp',['myApp.controllers']);
var controllersApp = angular.module('myApp.controllers', []);
controllersApp.controller('DocumentController',function($scope){
$scope.message = "This is the message from Document controller";
})
controllersApp.controller('MyController', function ($scope) {
$scope.message = "This is the message from My Controller";
})