Angular,无法实例化模块:Uncaught Error:[$ injector:modulerr]

时间:2016-06-02 18:30:18

标签: javascript angularjs exception

我一直在我的控制台中收到此异常:

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.5/$injector/modulerr?p0=mainApp&p1=Error%3A…at%20g%20(http%3A%2F%2Flocalhost%2Fcinema%2Fjs%2Fangular.min.js%3A39%3A222)

........ angular.min.js:6

和html文件:

<!DOCTYPE HTML>
<html ng-app="mainApp">
<head>
<title>This cinema...</title>
<!-- ... other tags -->
<script src="js/angular.min.js"></script>
<script>
var mainApp = angular.module('mainApp', []);

mainApp.config(function ($routeProvider) {

});
</script>
<!-- REST of the HTML body, but no angular used below -->

如果我删除mainApp.config(func....,那么它正在运行。我不知道如何设置路线。路由提供程序的空函数会生成异常吗?

3 个答案:

答案 0 :(得分:2)

根据documentation

  

$ routeProvider需要安装ngRoute模块。

因此,您必须在html中包含以下脚本:

<script src="js/angular-route.min.js"></script>

并将依赖项添加到模块定义中:

var mainApp = angular.module('mainApp', ['ngRoute']);

答案 1 :(得分:1)

$routeProvider需要单独的角度模块ngRoute。下载angular-route.js,将其添加到页面,并将其注入angular.module方法

<!DOCTYPE HTML>
<html ng-app="mainApp">
<head>
<title>This cinema...</title>
<!-- ... other tags -->
<script src="js/angular.min.js"></script>
<script src="js/angular-route.min.js"></script>
<script>
var mainApp = angular.module('mainApp', ['ngRoute']);

mainApp.config(function ($routeProvider) {

});
</script>
<!-- REST of the HTML body, but no angular used below -->

答案 2 :(得分:1)

您可以尝试使用$routeProvider而不使用ngRoute模块。

请参阅文档:https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

试试这个:

<!DOCTYPE HTML>
<html ng-app="mainApp">
<head>
<title>This cinema...</title>
<!-- ... other tags -->
<script src="js/angular.min.js"></script>
<script src="js/angular-route.min.js"></script>
<script>
var mainApp = angular.module('mainApp', ['ngRoute']);

mainApp.config(function ($routeProvider) {

});
</script>
<!-- REST of the HTML body, but no angular used below -->