2015-01-20 17 views
8

Tôi đang sử dụng máy chủ Apache để lưu trữ ứng dụng góc cạnh. Đây là 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> 

Khi tôi nhấn html từ trình duyệt, nó cho thấy một trang trống với lỗi này:

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.

Điều gì có thể xảy ra?

+2

điều này là do bạn không đóng thẻ tập lệnh. – Ved

+0

Wow. những gì một phiếu. cảm ơn người đàn ông! – moaglee

Trả lời

9

Lỗi là do giá trị trùng lặp bên trong mảng. Tôi đã thêm track by $index bên trong ng-repeat để giải quyết vấn đề này.

DOCS: ng-repeat

đổi mã:

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

tôi đã thêm bản nhạc theo $ index bên trong ng-repeat vì mảng của bạn chứa các giá trị trùng lặp. – Ved

0

Đặt phần <script> và kết thúc của cơ thể:

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

đó là một practicte tốt trong HTML và p articularly in Angular để đặt định nghĩa tệp JS của bạn ngay trước khi phần thân đã kết thúc

1

Vui lòng bao gồm các angularj trong cơ thể.

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

Bao gồm dòng này trong nội dung. Hy vọng nó sẽ hoạt động!

Các vấn đề liên quan