node.js AngularJS/Jade 错误:参数“MyController”不是函数,未定义(MEAN)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19719540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:09:30  来源:igfitidea点击:

AngularJS/Jade Error: Argument 'MyController' is not a function, got undefined (MEAN)

javascriptnode.jsangularjspug

提问by Chris B.

I know variations of this question have already been asked several times, but I've tried several suggested solutions for other OPs, haven't been able to resolve this, and would appreciate some clarification.

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

I'm working with the basic mean todo list app (http://www.mean.io/). After implementing a simple controller I'm running into the "Error: Argument 'nameOfMyController' is not a function, got undefined."

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

Here's where I'm at:

这是我所在的位置:

app.js (boilerplate)

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', []);

I've tried modifying what's referenced here, like, for example, adding mean.controller to the array but that clearly isn't the right way to do it because it tells me the module doesn't exist.

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

here's my taskController.js (a mean tutorial I followed made taskController a standalone function but I'm declaring it as a constructor the way angular's docs say to)

这是我的 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;
   };

});

I also tried angular.module('mean.system').controller('taskController', function taskController($scope){ ... }, same result.

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

ok now for the views: included ng-app in default.jade

现在可以查看视图:在 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

then in index.jade I have:

然后在 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 inserts:

脚.玉插入:

//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')

I don't thinkI'm missing any angular directives and the controller itself should work, so I'm assuming this is a basic app architecture problem where I don't understand how things fit together. Any help would be appreciated.

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

回答by Chandermani

This error mostly comes when angularjs is not able to locate the controller in any module. This can happen when you accidentally redefine the module. In your case i see that

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

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

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

and then

进而

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

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

This syntax redefine the module.

此语法重新定义了模块。

To get the existing module using angular.module('mean')without the second parameter.

angular.module('mean')没有第二个参数的情况下获取现有模块。

回答by Latin Warrior

I just had this issue now. I tried all the suggestions above and nothing worked. Then, I downgraded AngularJS from version 1.3.9-build.3746 to version 1.2.8. That is what I get for using beta versions.

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