我一直在尝试一个 ng-route 示例,该示例适用于 AngularJS 1.0.1 版,但不适用于 1.2.15 版。我确实添加了依赖 angular-route.js 但仍然出现未捕获对象错误。以下是我的例子。它由 4 个文件(index.html、page.html、chapter.html 和 main.html)组成。
如果您注释掉 1.2.15/angular.min.js、angular-route.js 和 .module('testNgRoute', ['ngRoute']) 并取消注释 1.0.1/angular.min.js 和 .module('testNgRoute ',[])。它会起作用的。
page.html 只包含一行“This is page.html”
chapter.html 仅包含一行“This is chapter.html”
main.html 仅包含一行“This is main.html”
这是我的 index.html 文件
<!DOCTYPE html>
<html ng-app="testNgRoute">
<title>Test ng-route</title>
<body>
<div>
<h1>Test ng-route</h1>
<div ng-view></div>
</div>
</body>
<!--it works when I use this version of angularJS-->
<!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>-->
<!--it didn't work when I use this version of angularJS-->
<!-- According to the documentation, I should include angular-route.js-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-route.js"><script>
<script>
angular
//it works when I use v1.0.1 and without including any dependencies.
//.module('testNgRoute', [])
//it didn't work when I use v1.2.15 with the "ngRoute" dependencies
.module('testNgRoute', ['ngRoute'])
.config(['$routeProvider', function ($routeProvider) {
"use strict";
$routeProvider
.when('/page', {
templateUrl: "page.html"
})
.when('/chapter', {
templateUrl: 'chapter.html'
})
.when('/', {
templateUrl: 'main.html'
});
}]);
</script>
</html>