admin管理员组文章数量:1347158
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?
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?
Share Improve this question edited Mar 21, 2016 at 20:19 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Mar 21, 2016 at 20:01 DeekorDeekor 9,49918 gold badges74 silver badges124 bronze badges 2-
You need to put
var app = angular.module("myApp");
in the process.js before theapp.controller(...)
. – Shaohao Commented Mar 21, 2016 at 20:06 -
@ShaohaoLin shouldn't
app
be available from the file included before hand? The error isn't plaining thatapp
doesn't exist. – Deekor Commented Mar 21, 2016 at 20:08
4 Answers
Reset to default 5Use 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.
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 }}
.
At the top of your app.js
var app = angular.module('myApp', ['myApp.controllers']);
At the top of your processor.js
var app = angular.module('myApp.controllers', []);
'use strict' ; Defines that JavaScript code should be executed in "strict mode". so it needs declaration first then definition . So in case you have declared the module added in app.js & added js to the index.html but not injected in the mainApp module , you have chances for ngareq ControllerName is not a function or undefined. Hope so it would help .
本文标签:
版权声明:本文标题:javascript - Error: [ng:areq] Argument 'ControllerName' is not a function, got undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743833578a2546973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论