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

前端 未结 24 1549
抹茶落季
抹茶落季 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:50

    I was getting the error because i had added the controller script before the script where i had defined the corresponding module in the app. First add the script

    <script src = "(path of module.js file)"></script>
    

    Then only add

    <script src = "(path of controller.js file)"></script>
    

    In the main file.

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

    If ALL ELSE fails and your running locally on the MEAN stack like me with gulp...just stop and serve again! I was pulling my hear out meticulously checking everything from all of your posts to no avail till I simply re-ran gulp serve.

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

    Same thing for me, comma ',' before function helped me in fixing the issue -- Error: ng:areq Bad Argument

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

    I also encountered this same error and the fix for me was to include my child module in the main module array.

    var myApp = angular.module('myApp', ['ngRoute', 'childModuleName']);
    
    0 讨论(0)
  • 2020-11-27 12:52

    I accidentally moved my HomeController.js out of the directly, where it was expected. Putting it again on original location.

    After that my website started to load pages automatically every second, I was even unable to look at the error. So i cleared the browser cache. It solved the problem

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

    Try this

        <title>My First Angular App</title>
    
    </head>
    
    <body>
    
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
        <h3>Adding Simple Controller<h3>        
    
        <div ng-controller="SimpleController">
            Name:
    
            <br/>
    
            <input type = "text" data-ng-model = "name"/> {{name}}
    
            <br/>
    
            <ul>    
                <li data-ng-repeat = "cust in customers | filter:name | orderBy:'city'">
                    {{cust.name}} - {{cust.city}}
                </li>
            </ul>
    
        </div>
    
    
        <script>
    
            var angularApp = angular.module('angularApp',[]);
    
            angularApp.controller('SimpleController', [ '$scope', SimpleController]);
    
            function SimpleController($scope)
            {
    
                $scope.customers = [
                                        {name:'Nikhil Mahirrao', city:'Pune'},
                                        {name:'Kapil Mahire', city:'Pune'},
                                        {name:'Narendra Mahirrao', city:'Phophare'},
                                        {name:'Mithun More', city:'Shahada'}
    
                                    ]; 
            }
    
        </script>
    
    </body>
    

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