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

前端 未结 24 1500
抹茶落季
抹茶落季 2020-11-27 12:22

This is my demo using angularjs, for creating a service file, and adding service to a controller.

I have two problems with my demo:

  • One is when I put <
相关标签:
24条回答
  • 2020-11-27 12:42

    In my case, I was missing the name of the Angular application in the html file. For example, I had included this file to be start of my application code. I had assumed it was being ran, but it wasn't.

    app.module.js

    (function () {
        'use strict';
    
        angular
            .module('app', [
            // Other dependencies here...
            ])
        ;
    
    })();
    

    However, when I declared the app in the html I had this:

    index.html

    <html lang="en" ng-app>
    

    But to reference the Angular application by the name I used, I had to use:

    index.html (Fixed)

    <html lang="en" ng-app="app">
    
    0 讨论(0)
  • 2020-11-27 12:42

    Error: ng:areq Bad Argument has gotten me a couple times because I close the square bracket too soon. In the BAD example below it is closed incorrectly after '$state' when it should actually go before the final parenthese.

    BAD:

    sampleApp.controller('sampleApp', ['$scope', '$state'], function($scope, $state){
    
    });
    

    GOOD:

    sampleApp.controller('sampleApp', ['$scope', '$state', function($scope, $state){
    
    }]);
    
    0 讨论(0)
  • 2020-11-27 12:43

    I also encountered this problem in my project. It eventually worked after I inserted the my-controller.js into my karma.conf.js file, with the <script> tag.

    Hope this will help. There are quite many reasons that can lead to this problem.

    0 讨论(0)
  • 2020-11-27 12:44

    Obviously that previous posts are useful, but any of above are not helpful in my case. The reason was in wrong sequence of loading scripts. For example, in my case, controller editCtrl.js depends on (uses) ui-bootstrap-tpls.js, so it should be loaded first. This caused an error:

    <script src="scripts/app/station/editCtrl.js"></script>
    <script src="scripts/angular-ui/ui-bootstrap-tpls.js"></script>
    

    This is right, works:

    <script src="scripts/angular-ui/ui-bootstrap-tpls.js"></script>
    <script src="scripts/app/station/editCtrl.js"></script>
    

    So, to fix the error you need first declare all scripts without dependencies, and then scripts that depends on previously declared.

    0 讨论(0)
  • 2020-11-27 12:44

    Check if your HTML page includes:

    1. angular.min script
    2. app.js
    3. controller JavaScript page

    The order the files are included is important. It was my solution to this problem.

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 12:46

    I had similar issue. The fix was ensure that your ctrollers are not only defined within script tags toward the bottom of your index.html just before the closing tag for body but ALSO validating that they are in order of how your folder is structured.

    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/controllers/Administration.js"></script>
    <script src="scripts/controllers/Leaderboard.js"></script>
    <script src="scripts/controllers/Login.js"></script>
    <script src="scripts/controllers/registration.js"></script>
    
    0 讨论(0)
提交回复
热议问题