Controller not a function, got undefined, while defining controllers globally

后端 未结 14 1776
感动是毒
感动是毒 2020-11-22 00:22

I am writing a sample application using angularjs. i got an error mentioned below on chrome browser.

Error is

Error: [ng:areq] http://errors.

相关标签:
14条回答
  • 2020-11-22 00:36

    Really great advise, except that the SAME error CAN occur simply by missing the critical script include on your root page

    example:

    page: index.html

       np-app="saleApp"
    

    Missing

    <script src="./ordersController.js"></script>
    

    When a Route is told what controller and view to serve up:

     .when('/orders/:customerId', {
         controller: 'OrdersController',
         templateUrl: 'views/orders.html'
     })
    

    So essential the undefined controller issue CAN occur in this accidental mistake of not even referencing the controller!

    0 讨论(0)
  • 2020-11-22 00:38

    If you're using routes (high probability) and your config has a reference to a controller in a module that's not declared as dependency then initialisation might fail too.

    E.g assuming you've configured ngRoute for your app, like

    angular.module('yourModule',['ngRoute'])
    .config(function($routeProvider, $httpProvider) { ... });
    

    Be careful in the block that declares the routes,

    .when('/resourcePath', { 
    templateUrl: 'resource.html',
    controller: 'secondModuleController' //lives in secondModule
    });
    

    Declare secondModule as a dependency after 'ngRoute' should resolve the issue. I know I had this problem.

    0 讨论(0)
  • 2020-11-22 00:40

    This error might also occur when you have a large project with many modules. Make sure that the app (module) used in you angular file is the same that you use in your template, in this example "thisApp".

    app.js

    angular
    .module('thisApp', [])
        .controller('ContactController', ['$scope', function ContactController($scope) {
            $scope.contacts = ["abcd@gmail.com", "abcd@yahoo.co.in"];
    
            $scope.add = function() {
                $scope.contacts.push($scope.newcontact);
                $scope.newcontact = "";
    
            };
        }]);
    

    index.html

      <html>
        <body ng-app='thisApp' ng-controller='ContactController>
             ...
            <script type="text/javascript" src="assets/js/angular.js"></script>
            <script src="app.js"></script>
        </body>
        </html>
    
    0 讨论(0)
  • 2020-11-22 00:40

    If all else fails and you are using Gulp or something similar...just rerun it!

    I wasted 30mins quadruple checking everything when all it needed was a swift kick in the pants.

    0 讨论(0)
  • 2020-11-22 00:47

    I had this error because I didn't understand the difference between angular.module('myApp', []) and angular.module('myApp').

    This creates the module 'myApp' and overwrites any existing module named 'myApp':

    angular.module('myApp', [])

    This retrieves an existing module 'myApp':

    angular.module('myApp')

    I had been overwriting my module in another file, using the first call above which created another module instead of retrieving as I expected.

    More detail here: https://docs.angularjs.org/guide/module

    0 讨论(0)
  • 2020-11-22 00:50

    I got this problem because I had wrapped a controller-definition file in a closure:

    (function() {
       ...stuff...
    });
    

    But I had forgotten to actually invoke that closure to execute that definition code and actually tell Javascript my controller existed. I.e., the above needs to be:

    (function() {
       ...stuff...
    })();
    

    Note the () at the end.

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