Error: [ng:areq] from angular controller

后端 未结 20 1975
攒了一身酷
攒了一身酷 2020-11-27 17:39

This is a long shot, but has anyone seen this error before? I am trying to add \'Transporters\' using express, angular and mongoDB. I get this error whenever I access a page

相关标签:
20条回答
  • 2020-11-27 18:30

    In my case I included app.js below the controller while app.js should include above any controller like

    <script src="js/app.js"></script> 
    <script src="js/controllers/mainCtrl.js"></script>
    
    0 讨论(0)
  • 2020-11-27 18:31

    I had this error too, I changed the code like this then it worked.

    html

     <html ng-app="app">
    
       <div ng-controller="firstCtrl">
           ...
       </div>
    
     </html>
    

    app.js

    (function(){
    
        var app = angular.module('app',[]);
    
        app.controller('firstCtrl',function($scope){    
             ...
        })
    
    })();
    

    You have to make sure that the name in module is same as ng-app

    then div will be in the scope of firstCtrl

    0 讨论(0)
  • 2020-11-27 18:31

    There's also another way this could happen.

    In my app I have a main module that takes care of the ui-router state management, config, and things like that. The actual functionality is all defined in other modules.

    I had defined a module

    angular.module('account', ['services']);
    

    that had a controller 'DashboardController' in it, but had forgotten to inject it into the main module where I had a state that referenced the DashboardController.

    Since the DashboardController wasn't available because of the missing injection, it threw this error.

    0 讨论(0)
  • 2020-11-27 18:32

    I've gotten that error twice:

    1) When I wrote:

    var app = module('flapperNews', []);
    

    instead of:

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

    2) When I copy and pasted some html, and the controller name in the html did not exactly match the controller name in my app.js file, for instance:

    index.html:

    <script src="app.js"></script>
    ...
    ...
    <body ng-app="flapperNews" ng-controller="MainCtrl">
    

    app.js:

    var app = angular.module('flapperNews', []);
    
    app.controller('MyCtrl', ....
    

    In the html, the controller name is "MainCtrl", and in the js I used the name "MyCtrl".

    There is actually an error message embedded in the error url:

    Error: [ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined

    Here it is without the hieroglyphics:

    MainCtrl not a function got undefined

    In other words, "There is no function named MainCtrl. Check your spelling."

    0 讨论(0)
  • 2020-11-27 18:32

    you forgot to include the controller in your index.html. The controller doesn't exist.

    <script src="js/controllers/Controller.js"></script>
    
    0 讨论(0)
  • 2020-11-27 18:34

    I had the same error in a demo app that was concerned with security and login state. None of the other solutions helped, but simply opening a new anonymous browser window did the trick.

    Basically, there were cookies and tokens left from a previous version of the app which put AngularJS in a state that it was never supposed to reach. Hence the areq assertions failed.

    0 讨论(0)
提交回复
热议问题