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

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

我正在使用 Apache 服务器托管 angular 应用程序。 这是 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>
   

当我从浏览器中点击 html 时,它显示一个空白页面并出现以下错误:

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.

有什么问题吗?

错误是因为数组中有重复值。我在 ng-repeat 中添加了 track by $index 来解决这个问题。

DOCS: ng-repeat

修改后的代码:

<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>

<script>部分和正文的结尾:

<body ng-app="myapp">

      <div ng-controller="indexCtrl">
        <div ng-repeat="word in words">
          {{word}}
        </div>
      </div>
      <script>
          myapp = angular.module('myapp', []);
          myapp.controller('indexCtrl', function($scope){
               $scope.words = ['It','is','what','it','is']
          });
    </script>
  </body>

在 HTML 中,特别是在 Angular 中,将 JS 文件的定义放在正文结束之前

是一个很好的做法

请在正文中包含 angularjs。

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

在正文中包含这一行。希望有用!