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

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

    My controller file was cached as empty. Clearing the cache fixed it for me.

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

    I also experienced this error but in my case it was because of controller naming convention. I declared controller: "QuestionController" in .state but in controller definition I declared it like

    yiiExamApp.controller('questionController' ...
    

    but it should be

    yiiExamApp.controller('QuestionController' ...
    

    hope that helps to people facing this error because of this stupid mistake I wasted 4hour in identifying it.

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

    Yes. As many have previously pointed out, I have added the src path to all the controller files in the index.html.

    <script src="controllers/home.js"></script>
    <script src="controllers/detail.js"></script>
    <script src="controllers/login.js"></script>
    <script src="controllers/navbar.js"></script>
    <script src="controllers/signup.js"></script>

    This fixed that error.

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

    This creates a new module/app:

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

    While this accesses an already created module (notice the omission of the second argument):

    var myApp = angular.module('myApp');
    

    Since you use the first approach on both scripts you are basically overriding the module you previously created.

    On the second script being loaded, use var myApp = angular.module('myApp');.

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

    Also check for spelling mistakes.

    var MyApp = angular.module('AppName',[]);
    MyApp.controller('WRONG_SPELLING_MyCtrl', ['$scope', MyControllerCtrl])
    function MyControllerCtrl($scope) {
       var vm = $scope;
       vm.Apple = 'Android';
    }
    
    <div ng-controller="ACTUAL_SPELLING_MyCtrl">
        {{Apple}}
    </div>
    
    0 讨论(0)
  • 2020-11-27 12:42

    Happened to me few times whenever I miss "," between list of injections and function

    app.controller('commonCtrl', ['$scope', '$filter',function($scope,$filter) {
    
    }]);
    
    0 讨论(0)
提交回复
热议问题