AngularJS error: 'argument 'FirstCtrl' is not a function, got undefined'

前端 未结 16 1127
时光取名叫无心
时光取名叫无心 2020-12-01 05:55

I noticed the same question was asked a few times here, I tried so solve it but nothing helps.

I\'m following this tutorial with the egghead videos.

But whe

相关标签:
16条回答
  • 2020-12-01 06:18

    I have faced this issue and it fixed with following way:

    1. first remove ng-app from:

      <html ng-app>
      
    2. add name of ng-app to myApp:

      <div ng-app="myApp">
      
    3. add this line of code before function:

      angular.module('myApp', []).controller('FirstCtrl',FirstCtrl);
      

    final look of script:

    angular.module('myApp', []).controller('FirstCtrl',FirstCtrl);
    
    function FirstCtrl($scope){
        $scope.data = {message: "Hello"};
    } 
    
    0 讨论(0)
  • 2020-12-01 06:19

    In my case, this message comes from forgotten dependency injection in main module

    0 讨论(0)
  • 2020-12-01 06:19

    i faced this issue but i was able to correct this issue by renaming the controller, please have a try on it.

    ctrlSub.execSummaryDocuments = function(){};
    
    0 讨论(0)
  • 2020-12-01 06:20

    I got exactly the same error message and in my case it turned out i didn't list the controller JS file (e.g. first-ctrl.js) in my index.html

    0 讨论(0)
  • 2020-12-01 06:24

    Another nice one: Accidentally redefining modules. I copy/pasted stuff a little too eagerly earlier today and ended up having a module definition somewhere, that I overrode with my controller definitions:

    // controllers.js - dependencies in one place, perfectly fine
    angular.module('my.controllers', [/* dependencies */]);
    

    Then in my definitions, I was supposed to reference it like so:

    // SomeCtrl.js - grab the module, add the controller
    angular.module('my.controllers')
     .controller('SomeCtrl', function() { /* ... */ });
    

    What I did instead, was:

    // Do not try this at home!
    
    // SomeCtrl.js
    angular.module('my.controllers', []) // <-- redefined module, no harm done yet
      .controller('SomeCtrl', function() { /* ... */ });
    
    // SomeOtherCtrl.js
    angular.module('my.controllers', []) // <-- redefined module - SomeCtrl no longer accessible
      .controller('SomeOtherCtrl', function() { /* ... */ });
    

    Note the extra bracket in the call to angular.module.

    0 讨论(0)
  • 2020-12-01 06:24

    I am not sure about this tutorial but I had the same problem when I forgot to include the file into grunt/gulp minimization process.

    grunt.initConfig({
      uglify: {
        my_target: {
          files: {
            'dest/output.min.js': ['src/input1.js', 'src/missing_controller.js']
          }
        }
      }
    });
    

    Hope that helps.

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