9

我知道这个问题的变体已经被问过好几次了,但我已经为其他 OP 尝试了几个建议的解决方案,但无法解决这个问题,希望能得到一些澄清。

我正在使用基本的平均待办事项列表应用程序(http://www.mean.io/)。在实现了一个简单的控制器后,我遇到了“错误:参数 'nameOfMyController' 不是函数,未定义。”

这是我所在的位置:

app.js(样板文件)

window.app = angular.module('mean', ['ngCookies', 'ngResource', 'ui.bootstrap', 'ui.route', 'mean.system', 'mean.articles' ]);

angular.module('mean.system', []);
angular.module('mean.articles', []);

我尝试修改此处引用的内容,例如,将 mean.controller 添加到数组中,但这显然不是正确的方法,因为它告诉我模块不存在。

这是我的 taskController.js (我遵循的一个平均教程使 taskController 成为一个独立的函数,但我将它声明为一个构造函数,就像 Angular 的文档所说的那样)

var mean = angular.module('mean',[]);

mean.controller('taskController', function taskController($scope){

   $scope.todos = [];

   $scope.doneFilter = { done : true };
   $scope.notDoneFilter = { done : false };

   $scope.setTodos = function(todos){
       $scope.todos = todos;
   };

});

我还尝试了 angular.module('mean.system').controller('taskController', function taskController($scope){ ... },结果相同。

现在可以查看视图:在 default.jade 中包含 ng-app

!!! 5
  html(ng-app='mean', lang='en', xmlns='http://www.w3.org/1999/xhtml', xmlns:fb='https://www.facebook.com/2008/fbml', itemscope='itemscope', itemtype='http://schema.org/Product')
    include ../includes/head
    body
     ...
     include ../includes/foot

然后在 index.jade 我有:

extends layouts/default

block head
  script(type='text/javascript', src='/js/controllers/taskController.js')

block content
  section(data-ng-view)
  div.container(ng-controller="taskController", ng-init="setTodos( #{JSON.stringify(todos)} )")
  ...
  div.row(ng-repeat="todo in todos | filter:notDoneFilter")
    label.checkbox
      input(type="checkbox", ng-model="todo.done")
      | {{ todo.description }}
    span.datestring
      i {{ todo.due | date: 'MMM d, yyyy' }}    
  ...
  div.row(ng-repeat="todo in todos | filter:doneFilter")
    label.checkbox
      input(type="checkbox", checked="true")
        del {{ todo.description }}
      span.datestring
        i {{ todo.due | date: 'MMM d, yyyy' }}

foot.jade 插入:

//AngularJS
script(type='text/javascript', src='/lib/angular/angular.js')
script(type='text/javascript', src='/lib/angular-cookies/angular-cookies.js')
script(type='text/javascript', src='/lib/angular-resource/angular-resource.js')

认为我没有遗漏任何角度指令,并且控制器本身应该可以工作,所以我假设这是一个基本的应用程序架构问题,我不明白事情是如何组合在一起的。任何帮助,将不胜感激。

4

2 回答 2

30

当 angularjs 无法在任何模块中找到控制器时,通常会出现此错误。当您不小心重新定义模块时,可能会发生这种情况。在你的情况下,我看到了

window.app = angular.module('mean', ['ngCookies', 'ngResource', 'ui.bootstrap', 'ui.route', 'mean.system', 'mean.articles' ]);

接着

var mean = angular.module('mean',[]);

这种语法重新定义了模块。

angular.module('mean')使用没有第二个参数的现有模块。

于 2013-11-01T04:41:15.653 回答
1

我现在才遇到这个问题。我尝试了上面的所有建议,但没有任何效果。然后,我将 AngularJS 从版本 1.3.9-build.3746 降级到版本 1.2.8。这就是我使用测试版所得到的。

于 2015-01-03T21:53:12.710 回答