2017-10-21 137 views
0

我正在使用Angularjs和Jasmine。如何解决错误'模块不可用!你拼错了......'

我得到以下错误:

Error: [$injector:modulerr] Failed to instantiate module myApplication due to: 
    Error: [$injector:nomod] Module 'myApplication' 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.4.4/$injector/nomod?p0=myApplication 
     at file:///Users/Documents/angularjs/code/src/lib/scripts/1_4_4/angular.js:68:12 
... 
TypeError: Cannot read property 'amount' of undefined 
    at UserContext.<anonymous> (file:///Users/Documents/angularjs/code/spec/tests/index_09_controller.spec.js:13:23) 

我-template.html

<html ng-app="myApplication"> 

<head> 
    <script src="lib/scripts/1_4_4/angular.min.js"></script> 
</head> 

<body> 
    <div ng-controller="SimpleController"> 
     {{amount.length}} 
    </div> 

    <script> 
     var myModule = angular.module('myApplication', []); 

     myModule.controller('SimpleController', function($scope) { 
       $scope.amount = ['a','b','c']; 
     }); 
    </script> 
</body> 

</html> 

我-template.spec.js

describe('a simple controller', function(){ 
    var $scope; 

    //See API angular.mock.module 
    beforeEach(module('myApplication')); 

    beforeEach(inject(function($rootScope, $controller){ 
     $scope = $rootScope.$new(); 
     $controller('SimpleController',{$scope : $scope}); 
    })); 

    it('test scope amount', function(){ 
     expect($scope.amount.length).toBe(3); 
    }); 
}); 

SpecRunner.html

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Jasmine Spec Runner v2.8.0</title> 

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.8.0/jasmine_favicon.png"> 
    <link rel="stylesheet" href="lib/jasmine-2.8.0/jasmine.css"> 

    <script src="lib/jasmine-2.8.0/jasmine.js"></script> 
    <script src="lib/jasmine-2.8.0/jasmine-html.js"></script> 
    <script src="lib/jasmine-2.8.0/boot.js"></script> 
    <script src="src/lib/scripts/1_4_4/angular.js"></script> 
    <script src="src/lib/scripts/1_4_4/angular-mocks.js"></script> 

    <!-- include source files here... --> 
    <script src="src/my-template.html"></script> 

    <!-- include spec files here... --> 
    <script src="spec/tests/my-template.spec.js"></script> 

</head> 

<body> 
</body> 
</html> 

我错过了什么?

放弃这一行
[我只是要添加的下方,计算器就不会错误,说没有足够说明一些乱码如下:“Lorem存有悲坐阿梅德,consectetur adipiscing ELIT, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。Ut enim ad minim veniam,quis nostrud practitation ullamco laboris nisi ut aliquip ex ea commodo consequat。Duis aute irure dolor in reninderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。Excepteur sint “非常重要”,“非常重要”,“非常重要”,以及“非常重要”的意思。“]

回答

1

弄清楚了。我不得不从html中删除脚本并创建一个单独的.js文件。

我-template.js

var myModule = angular.module('myApplication', []); 

myModule.controller('SimpleController', function($scope) { 
     $scope.amount = ['a','b','c']; 
}); 

我-template.html

<html ng-app="myApplication"> 

<head> 
    <script src="lib/scripts/1_4_4/angular.min.js"></script> 
    <script src="my-template.js"></script> 
</head> 

<body> 
    <div ng-controller="SimpleController as ctrl"> 
     {{amount.length}} 
    </div> 
</body> 

</html> 

SpecRunner.html - 添加以下行

... 
<script src="src/my-template.js"></script> 
... 
相关问题