AngularJS 路由
ngRoute 模块能让您的应用程序成为单页应用程序。
在 AngularJS 中路由是什么?
如果您想导航到应用程序中的不同页面,但又希望该应用程序是 SPA(单页面应用程序),且无需重新加载页面,则可以使用 ngRoute 模块。
ngRoute 模块将应用程序 路由 到不同的页面,而无需重新加载整个应用程序。
导航到 "red.htm", "green.htm", 和 "blue.htm":
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!red">Red</a><a href="#!green">Green</a><a href="#!blue">Blue</a><div ng-view=""></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm"}).when("/red", {templateUrl : "red.htm"}).when("/green", {templateUrl : "green.htm"}).when("/blue", {templateUrl : "blue.htm"});});</script><p>点击链接导航到 "red.htm", "green.htm", "blue.htm", 或者返回 "main.htm"</p></body></html>
该怎么做?
要使应用程序为路由做好准备,必须包含 AngularJS 路由模块:
<script src="https://cankaoshouce.com/js/angular-route.js"></script>
然后,必须将 ngRoute 作为依赖项添加到应用程序模块中:
var app = angular.module("myApp", ["ngRoute"]);
现在,您的应用程序可以访问 route 模块,该模块提供 $routeProvider。
使用 $routeProvider 在应用程序中配置不同的路由:
app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm"}).when("/red", {templateUrl : "red.htm"}).when("/green", {templateUrl : "green.htm"}).when("/blue", {templateUrl : "blue.htm"});});
从哪里开始?
应用程序需要一个容器来放置路由提供的内容。
此容器是 ng-view 指令。
在应用程序中包含 ng-view 指令有三种不同的方式:
实例:
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!red">Red</a><a href="#!green">Green</a><a href="#!blue">Blue</a><div ng-view=""></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm"}).when("/red", {templateUrl : "red.htm"}).when("/green", {templateUrl : "green.htm"}).when("/blue", {templateUrl : "blue.htm"});});</script><p>点击链接</p><p>本例使用 ng-view 指令作为 DIV 元素的属性。</p></body></html>
实例:
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!red">Red</a><a href="#!green">Green</a><a href="#!blue">Blue</a><ng-view></ng-view><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm"}).when("/red", {templateUrl : "red.htm"}).when("/green", {templateUrl : "green.htm"}).when("/blue", {templateUrl : "blue.htm"});});</script><p>点击链接</p><p>本例使用 ng-view 指令作为 DIV 元素的属性。</p></body></html>
实例:
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!red">Red</a><a href="#!green">Green</a><a href="#!blue">Blue</a><div class="ng-view"></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm"}).when("/red", {templateUrl : "red.htm"}).when("/green", {templateUrl : "green.htm"}).when("/blue", {templateUrl : "blue.htm"});});</script><p>点击链接</p><p>本例使用 ng-view 指令作为 DIV 元素的属性。</p></body></html>
应用程序只能有一个 ng-view 指令,这将是路由提供的所有视图的占位符。
$routeProvider
使用 $routeProvider 您可以定义用户单击链接时要显示的页面。
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!london">City 1</a><a href="#!paris">City 2</a><p>点击链接阅读有关伦敦和巴黎的信息。</p><div ng-view=""></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm"}).when("/london", {templateUrl : "london.htm"}).when("/paris", {templateUrl : "paris.htm"});});</script></body></html>
使用应用程序的 config 方法定义 $routeProvider。当应用程序加载时,将执行在 config 方法中注册的工作。
控制器
使用 $routeProvider,您还可以为每个 "view" 定义一个控制器。
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!london">City 1</a><a href="#!paris">City 2</a><p>点击链接</p><p>请注意,每个 "view" 都有自己的控制器,每个控制器都给 "msg" 变量一个值。</p><div ng-view=""></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {templateUrl : "main.htm",}).when("/london", {templateUrl : "london.htm",controller : "londonCtrl"}).when("/paris", {templateUrl : "paris.htm",controller : "parisCtrl"});});app.controller("londonCtrl", function ($scope) {$scope.msg = "I love London";});app.controller("parisCtrl", function ($scope) {$scope.msg = "I love Paris";});</script></body></html>
"london.htm" 和 "paris.htm" 是普通的 HTML 文件,您可以像添加 AngularJS 应用程序的任何其他 HTML 部分一样添加 AngularJS 表达式。
这些文件看起来像这样:
london.htm
<h2>London</h2><h3>London is the capital city of England.</h3><p>It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p><p>{{msg}}</p>
paris.htm
<h2>Paris</h2><h3>Paris is the capital city of France.</h3><p>The Paris area is one of the largest population centers in Europe, with more than 12 million inhabitants.</p><p>{{msg}}</p>
模板
在前面的示例中,我们使用了 $routeProvider.when 方法中的 templateUrl 属性。
您还可以使用 template 属性,它允许您直接在属性值中编写 HTML,而不引用页面。
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!banana">Banana</a><a href="#!tomato">Tomato</a><p>点击链接更改内容。</p><p>ng-view 指令中显示的 HTML 写在 $routeProvider 的 template 属性中。当你使用方法时。</p><div ng-view=""></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/", {template : "<h2>Main</h2><p>Click on the links to change this content</p>"}).when("/banana", {template : "<h2>Banana</h2><p>Bananas contain around 75% water.</p>"}).when("/tomato", {template : "<h2>Tomato</h2><p>Tomatoes contain around 95% water.</p>"});});</script></body></html>
otherwise 方法
在前面的示例中,我们使用了 $routeProvider 的 when 方法。
您还可以使用 otherwise 方法,这是其他方法都无法匹配时的默认路由。
<!DOCTYPE html><html><script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script><script src="https://cankaoshouce.com/js/angular/angular-route.js"></script><body ng-app="myApp"><p><a href="#/!">Main</a></p><a href="#!banana">Banana</a><a href="#!tomato">Tomato</a><p>点击链接更改内容。</p><p>使用 "otherwise" 方法定义在没有单击任何链接时显示的内容。</p><div ng-view=""></div><script>var app = angular.module("myApp", ["ngRoute"]);app.config(function($routeProvider) {$routeProvider.when("/banana", {template : "<h2>Banana</h2><p>Bananas contain around 75% water.</p>"}).when("/tomato", {template : "<h2>Tomato</h2><p>Tomatoes contain around 95% water.</p>"}).otherwise({template : "<h2>Nothing</h2><p>Nothing has been selected</p>"});});</script></body></html>