Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In app.js I have:

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

in process.js which is included after app.js I have:

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

and in my HTML file I have a div

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

This is throwing an error in my console:

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

I'm pretty new to angular and have never used multiple files like this before. What am I doing wrong?

like image 538
Deekor Avatar asked Jan 22 '26 04:01

Deekor


2 Answers

Use angular.module("myApp") inside other JS file & don't forget to call function which will make sense to have IIFE pattern, which will help you to make ProcessController controller available.

Code

(function(){
  angular.module("myApp")
    .controller('ProcessController', ['$http', function($http){
        this.something = "Test"
    }]);
})(); //<-- () function should get called to self execute it.
like image 102
Pankaj Parkar Avatar answered Jan 24 '26 17:01

Pankaj Parkar


Change your process.js to:

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

Also consider to use the syntax: ng-controller="ProcessController as process" in order to have access to this.something varialble after in your template {{ process.something }}.

like image 36
slackmart Avatar answered Jan 24 '26 17:01

slackmart