Error: Argument is not a function, got undefined

前端 未结 17 1714
不思量自难忘°
不思量自难忘° 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:12

    Some times this error is a result of two ng-app directives specified in the html. In my case by mistake I had specified ng-app in my html tag and ng-app="myApp" in the body tag like this:

    <html ng-app>
      <body ng-app="myApp"></body>
    </html>
    
    0 讨论(0)
  • 2020-12-07 20:12

    Turns out it's the Cache of the browser, using Chrome here. Simply check the "Disable cache" under Inspect (Element) solved my problem.

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

    FIRST. check if you have correct controller in the route definitions, same as the controller names that you are defining

    communityMod.config(['$routeProvider',
      function($routeProvider) {
        $routeProvider.
          when('/members', {
            templateUrl: 'modules/community/views/members.html',
            controller: 'CommunityMembersCtrl'
          }).
          otherwise({
            redirectTo: '/members'
          });
      }]);
    
    communityMod.controller('CommunityMembersCtrl', ['$scope',
        function ($scope) {
            $scope.name = 'Hello community';
        }]);
    

    different controller names in this example will lead to errors, but this example is correct

    SECOND check if you have imported your javascript file:

    <script src="modules/community/controllers/CommunityMembersCtrl.js"></script>

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

    I had the same error message (in my case : "Argument 'languageSelectorCtrl' is not a function, got undefined").

    After some tedious comparison with Angular seed's code, I found out that I had previously removed a reference to the controllers module in app.js. (spot it at https://github.com/angular/angular-seed/blob/master/app/js/app.js)

    So I had this:

    angular.module('MyApp', ['MyApp.filters', 'MyApp.services', 'MyApp.directives'])

    This failed.

    And when I added the missing reference:

    angular.module('MyApp', ['MyApp.filters', 'MyApp.services', 'MyApp.controllers', 'MyApp.directives'])

    The error message disappeared and Angular could instanciate the controllers again.

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

    I have encountered the same problem and in my case it was happening as a result of this problem:

    I had the controllers defined in a separate module (called 'myApp.controllers') and injected to the main app module (called 'myApp') like this:

    angular.module('myApp', ['myApp.controllers'])
    

    A colleague pushed another controller module in a separate file but with the exact same name as mine (i.e. 'myApp.controllers' ) which caused this error. I think because Angular got confused between those controller modules. However the error message was not very helpful in discovering what is going wrong.

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

    In my case (having an overview page and an "add" page) I got this with my routing setup like below. It was giving the message for the AddCtrl that could not be injected...

    $routeProvider.
      when('/', {
        redirectTo: '/overview'
      }).      
      when('/overview', {
        templateUrl: 'partials/overview.html',
        controller: 'OverviewCtrl'
      }).
      when('/add', {
        templateUrl: 'partials/add.html',
        controller: 'AddCtrl'
      }).
      otherwise({
        redirectTo: '/overview'
      });
    

    Because of the when('/' route all my routes went to the overview and the controller could not be matched on the /add route page rendering. This was confusing because I DID see the add.html template but its controller was nowhere to be found.

    Removing the '/'-route when case fixed this issue for me.

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