【问题标题】:AngularJS/Jade Error: Argument 'MyController' is not a function, got undefined (MEAN)AngularJS/Jade 错误:参数“MyController”不是函数,未定义(MEAN)
【发布时间】:2013-11-01 01:40:53
【问题描述】:

我知道这个问题的变体已经被问过好几次了,但我已经为其他 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')

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

【问题讨论】:

    标签: javascript node.js angularjs pug


    【解决方案1】:

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

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

    然后

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

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

    使用angular.module('mean')获取现有模块,不带第二个参数。

    【讨论】:

    • 谢谢,我遇到了两个问题。您正确识别的是第一个。使用 angular.module('mean').controller('taskController', function taskController($scope){...} 并删除我对模块的第二个定义解决了函数未定义错误。我还缺少间距/标识我的 index.jade (ng-repeat 需要在 ng-controller/ng-init 声明中)。修复两者让一切正常工作。
    • 你刚刚救了我几个小时的痛苦。非常感谢。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 2016-10-28
      • 2013-10-24
      • 2016-11-12
      • 1970-01-01
      • 2015-10-20
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多