Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: [$injector:modulerr] Failed to instantiate module error on Angular.js App on Apache

I'm using Apache server to host an angular app. This is the index.html:

<html>
  <head>
    <script src="/lib/angular/angular.js">
  </head>
  <script>
     myapp = angular.module('myapp', []);
     
     myapp.controller('indexCtrl', function($scope){
    
          $scope.words = ['It','is','what','it','is']
     });
    
  </script>
  
  <body ng-app="myapp">

      <div ng-controller="indexCtrl">
        <div ng-repeat="word in words">
          {{word}}
        </div>
      </div>

  </body>
</html>
   

When I hit the html from the browser, it shows a blank page with this error :

Uncaught Error: [$injector:modulerr] Failed to instantiate module myapp due to: Error: [$injector:nomod] Module 'myapp' is not available!

You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

What could be wrong?

like image 691
vivekanon Avatar asked Jan 20 '15 09:01

vivekanon


2 Answers

The error is because of duplicate values inside array. I have added track by $index inside ng-repeat to resolve this issue.

DOCS: ng-repeat

Modified code:

<html>
  <head>
    <script src="/lib/angular/angular.js"></script>
  </head>
  <script>
     var myapp = angular.module('myapp', []);

     myapp.controller('indexCtrl', function($scope){

          $scope.words = ['It','is','what','it','is']
     });

  </script>

  <body ng-app="myapp">

      <div ng-controller="indexCtrl">

          <div ng-repeat="word in words track by $index">

             {{word}}

          </div>
      </div>

  </body>
</html>
like image 66
Ved Avatar answered Oct 10 '22 14:10

Ved


Please include angularjs in body.

<script src="/lib/angular/angular.js">

Include this line in body. Hope it will works!

like image 31
Lalit Sachdeva Avatar answered Oct 10 '22 12:10

Lalit Sachdeva