Error: Argument is not a function, got undefined

前端 未结 17 1713
不思量自难忘°
不思量自难忘° 2020-12-07 19:35

Using AngularJS with Scala Play, I\'m getting this error.

Error: Argument \'MainCtrl\' is not a function, got undefined

I\'m try

相关标签:
17条回答
  • 2020-12-07 20:01

    I had the same error with a big mistake:

    appFormid.controller('TreeEditStepControlsCtrl', [$scope, function($scope){
    
    }]);
    

    You see ? i forgot the '' around the first $scope, the right syntax is of course:

    appFormid.controller('TreeEditStepControlsCtrl', ['$scope', function($scope){
    
    }]);
    

    A first error i didn't see immediatly was: "$scope is not defined", followed by "Error: [ng:areq] Argument 'TreeEditStepControlsCtrl' is not a function, got undefined"

    0 讨论(0)
  • 2020-12-07 20:01

    In my case it was a simple typo in index.html:

    <script src="assets/javascript/controllers/questionssIndexController.js"></script>
    

    that should have been

    <script src="assets/javascript/controllers/questionsIndexController.js"></script>
    

    without the extra s in the controller's name.

    0 讨论(0)
  • 2020-12-07 20:06

    I got sane error with LoginController, which I used in main index.html. I found two ways to resolve:

    1. setting $controllerProvider.allowGlobals(), I found that comment in Angular change-list "this option might be handy for migrating old apps, but please don't use it in new ones!" original comment on Angular

      app.config(['$controllerProvider', function($controllerProvider) { $controllerProvider.allowGlobals(); }]);

    2. wrong contructor of registering controller

    before

    LoginController.$inject = ['$rootScope', '$scope', '$location'];
    

    now

    app.controller('LoginController', ['$rootScope', '$scope', '$location', LoginController]);
    

    'app' come from app.js

    var MyApp = {};
    var app = angular.module('MyApp ', ['app.services']);
    var services = angular.module('app.services', ['ngResource', 'ngCookies', 'ngAnimate', 'ngRoute']);
    
    0 讨论(0)
  • 2020-12-07 20:09

    This seriously took me 4 HOURS (including endless searches on SO) but finally I found it: by mistake (unintentionally) I added a space somewhere.

    Can you spot it?

    angular.module('bwshopper.signup').controller('SignupCtrl ', SignupCtrl);

    So ... 4 hours later I saw that it should be:

    angular.module('bwshopper.signup').controller('SignupCtrl', SignupCtrl);

    Almost impossible to see with just the naked eye.

    This stresses the vital importance of revision control (git or whatever) and unit/regression testing.

    0 讨论(0)
  • 2020-12-07 20:09

    To fix this problem, I had to discover that I misspelled the name of the controller in the declaration of Angular routes:

    .when('/todo',{
                templateUrl: 'partials/todo.html',
                controller: 'TodoCtrl'
            })
    
    0 讨论(0)
  • 2020-12-07 20:11

    If you are in a submodule, don't forget to declare the module in main app. ie :

    <scrip>
    angular.module('mainApp', ['subModule1', 'subModule2']);
    
    angular.module('subModule1')
       .controller('MyController', ['$scope', function($scope) {
          $scope.moduleName = 'subModule1';
       }]);
    </script>
    ...
    <div ng-app="mainApp">
       <div ng-controller="MyController">
       <span ng-bind="moduleName"></span>
    </div>
    

    If you don't declare subModule1 in mainApp, you will got a "[ng:areq] Argument "MyController" is not a function, got undefined.

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