2017-02-18 80 views
0

這是我的HTML:模塊「mainApp」不可用

<!DOCTYPE html> 
<html ng-app="mainApp"> 
<head> 
    <title></title> 
    <link href="/Content/ui-grid.css" rel="stylesheet"/> 
</head> 
<body ng-controller="MainController"> 
    <h2>Welcome to ASP.NET MVC 5.2 on Mono!</h2> 
    <script src="/Scripts/angular-mocks.js"></script> 
    <script src="/Scripts/angular-resource.js"></script> 
    <script src="/Scripts/angular-route.js"></script> 
    <script src="/Scripts/angular.js"></script> 
    <script src="/Scripts/ui-grid.js"></script> 
    <script src="/App/Controllers/MainController.js"></script> 
    <script src="/App/App.js"></script> 
</body> 
</html> 

我檢查了資源選項卡,並確認所有的JS文件是否可用。

這是我App.js:

'use strict'; 

(function() { 
    angular 
    .module('mainApp', ['ngResource', 'ngRoute']) 
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { 
     $locationProvider.html5Mode(true); 
     debugger; 
     $routeProvider 
     .when('/events', 
     { 
      templateUrl: 'App/Templates/Main.html', 
      controller: 'MainController' 
     }) 
     .otherwise({redirectTo: 'events'}); 
    }]); 
})(); 

這是MainController.js:

'use strict'; 

(function() { 
    angular 
    .module('mainApp') 
    .controller('MainController', ['$scope', '$location', function($scope, $location) { 
     $scope.goHome = function() { 
      $location.replace(); 
      $location.url('/'); 
     }; 
     $scope.createEvent = function() { 
      $location.replace(); 
      $location.url('/newEvent'); 
     }; 
     $scope.listAllEvents = function() { 
      $location.replace(); 
      $location.url('/events'); 
     }; 
    }]); 
})(); 

我目前得到的瀏覽器控制檯以下錯誤:

TypeError: undefined is not an object (evaluating 'angular.mock = {}') 

TypeError: undefined is not an object (evaluating 'angular.$$minErr') 
(anonymous function) — angular-resource.js:8 
Global Code — angular-resource.js:847 

TypeError: undefined is not an object (evaluating 'angular. 
    module') 

Error: [$injector:nomod] Module 'mainApp' 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. 
http://errors.angularjs.org/1.6.1/$injector/nomod?p0=mainApp 

Error: [$injector:modulerr] Failed to instantiate module mainApp due to: 
[$injector:modulerr] Failed to instantiate module ngResource due to: 
[$injector:nomod] Module 'ngResource' 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. 

我應該如何解決錯誤並使mainApp加載?

回答

1

我認爲問題出在你已經包含在你的HTML文件的序列。因爲它們都依賴於角

嘗試以下

<script src="/Scripts/angular.js"></script> 
<script src="/Scripts/angular-mocks.js"></script> 
<script src="/Scripts/angular-resource.js"></script> 
<script src="/Scripts/angular-route.js"></script> 
<script src="/Scripts/ui-grid.js"></script> 
<script src="/App/App.js"></script> 
<script src="/App/Controllers/MainController.js"></script> 
+0

感謝的順序,不能相信我錯過了你的角度應該出現在任何你的其它腳本文件。應該/App/App.js src元素最後一次?我現在唯一的控制檯錯誤是:錯誤:[$ injector:nomod]模塊'mainApp'不可用!您拼錯了模塊名稱或忘記加載模塊名稱。如果註冊模塊確保您指定依賴關係作爲第二個參數。 http://errors.angularjs.org/1.6.1/$injector/nomod?p0=mainApp –

+0

應用程序/ App.js應該是倒數第二,MainController應該如我所說的最後一個。 –

+0

非常感謝!我沒有注意到我的代碼中的最後一個與你提到的不一樣。所有的作品,並沒有在控制檯中的錯誤!你是個明星! –