index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AngularJS 路由实例 - 菜鸟教程</title>
<script src="https://cdn.bootcss.com/angular.js/1.7.0/angular.min.js"></script>
<script src="https://cdn.bootcss.com/angular.js/1.7.0/angular-route.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="ngRouteExample" class="ng-scope">
<div>
<div id="navigation">
<a href="#!/home">Home</a>
<a href="#!/about">About</a>
</div>
<div ng-view="">
</div>
</div>
</body>
</html>
app.js
angular.module('ngRouteExample', ['ngRoute'])
.controller('HomeController', function ($scope, $route) { $scope.$route = $route;})
.controller('AboutController', function ($scope, $route) { $scope.$route = $route;})
.config(function ($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'home.html',
controller: 'HomeController'
}).
when('/about', {
templateUrl: 'about.html',
controller: 'AboutController'
}).
otherwise({
redirectTo: '/home'
});
});
home.html
<script type="text/ng-template" id="home.html">
<h1> Home </h1>
</script>
about.html
<script type="text/ng-template" id="about.html">
<h1> About </h1>
</script>
加载之后显示如下
<html><head><style type="text/css">@charset "UTF-8";[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak,.ng-hide:not(.ng-hide-animate){display:none !important;}ng\:form{display:block;}.ng-animate-shim{visibility:hidden;}.ng-anchor{position:absolute;}</style>
<meta charset="utf-8">
<title>AngularJS 路由实例 - 菜鸟教程</title>
<script src="https://cdn.bootcss.com/angular.js/1.7.0/angular.min.js"></script>
<script src="https://cdn.bootcss.com/angular.js/1.7.0/angular-route.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="ngRouteExample" class="ng-scope">
<div>
<div id="navigation">
<a href="#!/home">Home</a>
<a href="#!/about">About</a>
</div>
<div ng-view="" class="ng-scope"><script type="text/ng-template" id="about.html" class="ng-scope">
<h1> About </h1>
</script></div><!-- ngView: -->
</div>
</body></html>
多了script这一层标签,在home和about之间来回多点击几次,又能显示正常。