我一直在尝试一个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>
答案 0 :(得分:0)
这是一个新手的错误。 angular-route.js脚本标记未正确关闭。感谢tasseKATT发现它。
误报。我已经测试过了,它运行正常。