Angularjs Uncaught Error: [$injector:modulerr] when migrating to V1.3

前端 未结 4 483
我寻月下人不归
我寻月下人不归 2020-11-22 10:47

I am learning Angular.js and I am not able to figure out whats wrong with this simple code. It seems to look fine but giving me following error.



        
相关标签:
4条回答
  • 2020-11-22 11:10

    I was myself stuck in this issue for sometime. Check for following in order:-

    1. The path to you angular.js script is correct (whether you are calling it in your HTML from a local source or as an external resource).

    2. Next, once your angular.js is correct check if your app is initialized or not.

      var app=angular.module('app',[])//in your app.js file

      <body ng-app="app">//in your html

    3. Next register your controller with the app and pass in all necessary injections

      app.controller('myCtrl',function(){});

    4. Call your javascript file in your html file

      <script src="app.js"></script>

    0 讨论(0)
  • 2020-11-22 11:20

    After AngularJS version 1.3 global controller function declaration is disabled

    You need to first create an AngularJS module & then attach all the components to that specific module.

    CODE

    function Ctrl($scope) {
        $scope.age = 24;
    }
    
    angular.module('app', [])
        .controller('Ctrl', ['$scope', Ctrl]);
    

    Specifically for your case, there is some issue with AngularJS 1.3.14 (downgrade it to 1.3.13 works fine). Though I'd prefer you to use angular 1.2.27 AngularJS 1.6.X, Which is more stable version & latest release of AngularJS.

    Working Plunkr

    UPDATE:

    You could do your current code to working state by allow global controller declaration inside angular.config. But this isn't the correct way to run angular application.

    function Ctrl($scope) {
        $scope.age = 24;
    }
    
    angular.module('app', [])
        .config(['$controllerProvider',
            function ($controllerProvider) {
                $controllerProvider.allowGlobals();
            }
        ]);
    
    0 讨论(0)
  • 2020-11-22 11:23

    You have to define your controller

    var app = angular.module('app', []);
    
    app.controller('Ctrl', ['$scope',function($scope) {
      $scope.age = 24;
    }]);
    
    0 讨论(0)
  • 2020-11-22 11:31

    Be Sure that ng-app="app_name" define must match to

    var app=angular.module('app_name',[])

    <html ng-app="myApp">
    <body>
      <div ng-controller="Ctrl">
          <input ng-model="name">
          <h1>{{name}}</h1>
          <h2>{{age}}</h2>
      </div>
      <script>
        var app = angular.module('myApp',[]); // same to the above define appName
        app.controller('Ctrl',function($scope){
          $scope.age=24; // initialize age by injecting scope
        });
      </script>
    </body>
    <html>
    

    For more details visit Here

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