使用AngularJS与Scala Play,我得到这个错误。
错误:Argument 'MainCtrl'不是一个函数,得到了未定义。
我试图创建一个由一周内的日子组成的表格。
请看一下我的代码。我已经检查了控制器的名称,但似乎是正确的。注:所用代码来自于这个SO答案。
index.scala.html
@(message: String)
@main("inTime") {
<!doctype html>
<html lang="en" ng-app>
<head>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
</head>
<div ng-controller="MainCtrl">
<table border="1">
<tbody ng-repeat='(what,items) in data'>
<tr ng-repeat='item in items'>
<td ngm-if="$first" rowspan="{{items.length}}">{{what}}</td>
<td>{{item}}</td>
</tr>
</tbody>
</table>
</div>
</html>
}
MainCtrl.js
(function() {
angular.module('[myApp]', []).controller('MainCtrl', function($scope) {
$scope.data = {
Colors: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
}
});
}());
移除模块名称([myApp])中的"[]"。
angular.module('myApp', [])
并在html中加入ng-app="myApp"
,应该就可以了。
首先。 检查你在路由定义中是否有正确的`controller',与你所定义的控制器名称相同
communityMod.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/members', {
templateUrl: 'modules/community/views/members.html',
controller: 'CommunityMembersCtrl'
}).
otherwise({
redirectTo: '/members'
});
}]);
communityMod.controller('CommunityMembersCtrl', ['$scope',
function ($scope) {
$scope.name = 'Hello community';
}]);
本例中不同的控制器名称会导致错误,但本例是正确的。
第二检查你是否已经导入了你的javascript文件:
``<script src="modules/community/controllers/CommunityMembersCtrl.js"></script>````。
我有同样的错误信息(在我的情况下:"参数'languageSelectorCtrl'不是一个函数,得到未定义")。
在与Angular seed'的代码进行了一些繁琐的比较后,我发现我之前删除了app.js中对controllers模块的引用。(发现它在https://github.com/angular/angular-seed/blob/master/app/js/app.js)
所以我就有了这个:
angular.module('MyApp', ['MyApp.filters', 'MyApp.services', 'MyApp.directives'])< /code>;
这个失败了。
而当我添加了缺少的引用时:
angular.module('MyApp', ['MyApp.filters', 'MyApp.services', 'MyApp.directives'] )
</code>;
错误信息消失了,Angular又可以实例化控制器了。