4

$injector:modulerr包含 ngRoute 后出现错误。使用 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>

4

1 回答 1

2

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

.config

您应该使用嵌套.when而不是再次定义$routeProvider

引号之间的控制器名称

);控制器中缺少关闭

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

于 2014-10-12T07:05:54.977 回答