Error: [ng:areq] Argument 'ControllerName' is not a function, got undefined

Error: [ng:areq] Argument 'ControllerName' is not a function, got undefined

app.js 我有:

(function(){
    var app = angular.module("myApp", []);
})();

in process.js 包含在 after app.js 我有:

(function(){
    app.controller('ProcessController', ['$http', function($http){
        this.something = "Test"
    }]);
});

在我的 HTML 文件中有一个 div

<html class="no-js" lang="en" ng-app="myApp">
...
   <div class="row" ng-controller="ProcessController">

这在我的控制台中引发错误:

Error: [ng:areq] Argument 'ProcessController' is not a function, got undefined

我是 angular 的新手,以前从未使用过这样的多个文件。我做错了什么?

将您的 process.js 更改为:

(function(){
    var app = angular.module("myApp");
    app.controller('ProcessController', ['$http', function($http){
        this.something = "Test"
    }]);
})();

同时考虑使用语法:ng-controller="ProcessController as process" 以便在您的模板 {{ process.something }}.

之后访问 this.something 变量

在其他 JS 文件中使用 angular.module("myApp") & 不要忘记调用具有 IIFE pattern 有意义的函数,这将帮助您使 ProcessController 控制器可用。

代码

(function(){
  angular.module("myApp")
    .controller('ProcessController', ['$http', function($http){
        this.something = "Test"
    }]);
})(); //<-- () function should get called to self execute it.

在您的 app.js

顶部
var app = angular.module('myApp', ['myApp.controllers']);

在您的 processor.js

顶部
var app = angular.module('myApp.controllers', []);

'use strict';定义 JavaScript 代码应在 "strict mode" 中执行。 所以它需要先声明然后定义。因此,如果您已声明在 app.js 中添加的模块并将 js 添加到 index.html 但未注入 mainApp 模块,则 ngareq ControllerName 可能不是函数或未定义。 希望对您有所帮助。