2014-12-06 70 views
9

收到以下錯誤在我的瀏覽器:模塊'ngMockE2E'不可用! AngularJS

Uncaught Error: [$injector:modulerr] Failed to instantiate module sayHiApp due to: 
Error: [$injector:modulerr] Failed to instantiate module ngMockE2E due to: 
Error: [$injector:nomod] Module 'ngMockE2E' 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.2.15/$injector/nomod?p0=ngMockE2E 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:78:12 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:1611:17 
    at ensure (http://127.0.0.1:9000/bower_components/angular/angular.js:1535:38) 
    at module (http://127.0.0.1:9000/bower_components/angular/angular.js:1609:14) 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3717:22 
    at Array.forEach (native) 
    at forEach (http://127.0.0.1:9000/bower_components/angular/angular.js:323:11) 
    at loadModules (http://127.0.0.1:9000/bower_components/angular/angular.js:3711:5) 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3718:40 
    at Array.forEach (native) 
http://errors.angularjs.org/1.2.15/$injector/modulerr?p0=ngMockE2E&p1=Error…ngular%2Fangular.js%3A3718%3A40%0A%20%20%20%20at%20Array.forEach%20(native) 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:78:12 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3745:15 
    at Array.forEach (native) 
    at forEach (http://127.0.0.1:9000/bower_components/angular/angular.js:323:11) 
    at loadModules (http://127.0.0.1:9000/bower_components/angular/angular.js:3711:5) 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3718:40 
    at Array.forEach (native) 
    at forEach (http://127.0.0.1:9000/bower_components/angular/angular.js:323:11) 
    at loadModules (http://127.0.0.1:9000/bower_components/angular/angular.js:3711:5) 
    at createInjector (http://127.0.0.1:9000/bower_components/angular/angular.js:3651:11) 
http://errors.angularjs.org/1.2.15/$injector/modulerr?p0=sayHiApp&p1=Error%…F%2F127.0.0.1%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A3651%3A11) angular.js:78 

我app.js看起來是這樣的:

'use strict'; 

angular 
    .module('sayHiApp', [ 
    'ngCookies', 
    'ngMockE2E', 
    'ngResource', 
    'ngSanitize', 
    'ngRoute' 
    ]) 
    .config(function ($routeProvider) { 
    $routeProvider 
     .when('/', { 
     templateUrl: 'views/main.html', 
     controller: 'MainCtrl' 
     }) 
     .otherwise({ 
     redirectTo: '/' 
     }); 
    }) 
    .run(function($httpBackend) { 

    var name = ''; 

    $httpBackend.whenPOST('/name').respond(function(method, url, data) { 
     name = angular.fromJson(data); 
     return [200, name, {}]; 
    }); 

    $httpBackend.whenGET('/name').respond(name); 

    }); 

我缺少的東西?

+11

您正在加載'angular-mocks.js'腳本文件嗎? – dfsq 2014-12-06 07:59:00

+0

何噢...我認爲這是問題..讓我快速涼亭安裝 – Tiwaz89 2014-12-06 08:12:13

+1

是的,這是錯誤。謝謝! – Tiwaz89 2014-12-06 08:22:08

回答

1

angular.mocks.js文件包含許多有助於模擬和測試的模塊。最常用的是ngMock,ngMockE2E,它們提供了一些最常用組件的模擬,如$timeout, $rootScope, $controller, $httpBackend($ httpBackend是ngMockE2E的一部分)。

爲了使用這些模塊中的任何一個作爲依賴性,您需要加載angular.mocks.js。只需在html文件中添加此腳本以刪除[$injector:modulerr],這是因爲未找到作爲依賴關係添加的模塊或其文件未加載導致的錯誤

0

我只添加使用$ httpBackend,因此只有'ngMocksE2E'模塊是必要的,所以我這樣做:

import angular from 'angular'; 
// it exports moduleName that is 'ngMockE2E' 
import ngMockE2EModuleName from 'angular-mocks/ngMocksE2E.js'; 

export default angular.module('app', [ngMockE2EModuleName]);