错误:[ng:areq]参数'ControllerName'不是函数,未定义

Dee*_*kor 3 javascript angularjs angularjs-controller

app.js我有:

(function(){
    var app = angular.module("myApp", []);
})();
Run Code Online (Sandbox Code Playgroud)

在我拥有之后process.js 包括: app.js

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

在我的HTML档案中,我有一个div

<html class="no-js" lang="en" ng-app="myApp">
...
   <div class="row" ng-controller="ProcessController">
Run Code Online (Sandbox Code Playgroud)

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

Error: [ng:areq] Argument 'ProcessController' is not a function, got undefined
Run Code Online (Sandbox Code Playgroud)

我对角度很新,从来没有使用过像这样的多个文件.我究竟做错了什么?

Pan*_*kar 5

angular.module("myApp")在其他JS文件中使用并且不要忘记调用具有IIFE模式的有意义的功能,这将帮助您使ProcessController控制器可用.

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

  • Upvote用于参考IIFE模式. (3认同)