包含 ngRoute 后出现 $injector:modulerr 错误。使用 AngularJS 1.2.26

var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.when('/', {controller: indexController1, templateURL: 'index1.html'});
    $routeProvider.when('/view/:id', {controller: indexController2, templateURL: 'index2.html'});
    $routeProvider.otherwise({redirectTo: '/'});
});
app.controller('indexController1', function ($scope) { .... }
app.controller('indexController2', function ($scope, $routeParams) { .... }

html模板
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.min.js"></script>
<script src="app.js">
</head>
<body>
<div ng-view></div>
</body>
</html>

最佳答案

您的代码中存在一些问题:

.config

您应该使用嵌套的 .when 而不是再次定义 $routeProvider
引号之间的 Controller 名称

在 Controller 中缺少关闭 );

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

app.config(function ($routeProvider) {
    $routeProvider
     .when('/', {
         templateUrl: 'index1.html',
         controller: 'indexController1'
     })
     .when('/view/:id', {
         templateUrl: 'index2.html',
         controller: 'indexController2'
     })
     .otherwise({
         redirectTo: '/'
     });
});

app.controller('indexController1', function ($scope) {

});

app.controller('indexController2', function ($scope, $routeParams) {

});

html

缺少 </script> 关闭标签。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.min.js"></script>
<script src="app.js"></script>

在这里检查一个工作示例 ngRoute

10-06 11:53