AngularJS 避繁就简的路由
程序员文章站
2022-06-17 16:02:27
angularjs 路由允许我们通过不同的 url 访问不同的内容。
通过 angularjs 可以实现多视图的单页web应用(single page web appli...
angularjs 路由允许我们通过不同的 url 访问不同的内容。
通过 angularjs 可以实现多视图的单页web应用(single page web application,spa)。
通常我们的url形式为 http://runoob.com/first/page,但在单页web应用中 angularjs 通过 # + 标记 实现,例如:
http://runoob.com/#/first
http://runoob.com/#/second
http://runoob.com/#/third
先看看$routeprovider 的配置对象属性方法:
路由设置对象解析:
$routeprovider.when(url(路由名称), { template: string(模板提示字符串), templateurl: string(模板路径url), controller: string, function 或 array(在当前模板创建控制器,生成新的 $scope 作用域), controlleras: string(控制器别名), redirectto: string, function(重定向地址), resolve: object<key, function>(当前路由所依赖的模块) });
实现路由的大致步骤:
第一步:导入ngroute模块
<script type="text/javascript" src="js/angular-route.min.js"></script>
第二步:在应用模块中使用ngroute
angular.module("routeapp", ["ngroute"])
第三步:使用 ng-view 指令
<div ng-view class="well" ng-controller='defaultctrl'></div>
第四步:配置 $routeprovider 路由规则
... .config(['$routeprovider', function ($routeprovider){ $routeprovider .when('/home', { templateurl : 'home.tpl.html', controller : 'homectrl', }) .when('/computer', { templateurl : 'computer.html', }) .when('/phone', { templateurl : 'phone.html', }) .when('/other', { templateurl : 'other.tpl.html', controller : 'otherctrl', }) }]) ...
第五步:通过超链接使用路由
<ul class="nav nav-tabs"> <li><a href="#/home">首页</a></li> <li><a href="#/computer">电脑</a></li> <li><a href="#/phone">手机</a></li> <li><a href="#/other">其他</a></li> </ul>
完整案例:
1 route.html页面
<html> <head> <meta charset="utf-8"> <title>angularjs 路由实例</title> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> </head> <body ng-app='routeapp'> <ul class="nav nav-tabs"> <li><a href="#/home">首页</a></li> <li><a href="#/computer">电脑</a></li> <li><a href="#/phone">手机</a></li> <li><a href="#/other">其他</a></li> </ul> <div ng-view class="well" ng-controller='defaultctrl'></div> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="js/angular-route.min.js"></script> <script type="text/ng-template" id="home.tpl.html"> <h1>{{data}}</h1> </script> <script type="text/ng-template" id="other.tpl.html"> <h1>{{data}}</h1> </script> <script type="text/javascript"> angular.module("routeapp", ["ngroute"]) .config(['$routeprovider', function ($routeprovider){ $routeprovider .when('/home', { templateurl : 'home.tpl.html', controller : 'homectrl', }) .when('/computer', { templateurl : 'computer.html', }) .when('/phone', { templateurl : 'phone.html', }) .when('/other', { templateurl : 'other.tpl.html', controller : 'otherctrl', }) }]) .controller('defaultctrl', function ($scope) { $scope.computers = [ { id: 0, name: "宏基", category: "test", price: 1.25 }, { id: 1, name: "联想", category: "test", price: 2.45 }, { id: 2, name: "苹果", category: "test", price: 4.25 } ]; $scope.phones = [ { id: 0, name: "三星", category: "test", price: 1.25 }, { id: 1, name: "荣耀", category: "test", price: 2.45 }, { id: 2, name: "魅族", category: "test", price: 4.25 } ]; }) .controller("homectrl", function ($scope, $route) { $scope.$route = $route; $scope.data = "home home"; }) .controller("otherctrl", function ($scope, $route) { $scope.$route = $route; $scope.data = "other other"; }) </script> </body> </html>
2.computer.html 页面
<div class="panel-body"> <table class="table table-striped table-hover"> <thead> <tr> <th>名称</th> <th>类别</th> <th class="text-right">价格</th> <th>{{data}}</th> </tr> </thead> <tbody> <tr ng-repeat="item in computers"> <td>{{item.name}}</td> <td>{{item.category}}</td> <td class="text-right">{{item.price | currency}}</td> <td class="text-center"> <button class="btn btn-xs btn-primary" ng-click="deleteproduct(item)">删除</button> <a href="/edit/{{item.id}}" class="btn btn-xs btn-primary" ng-click="editorcreateproduct(item)">编辑</a> <button class="btn btn-xs btn-primary" ng-click="incrementprice(item)">+</button> </td> </tr> </tbody> </table> <div> <button class="btn btn-xs btn-primary" ng-click="editorcreateproduct()">添加</button> <a href="create" class="btn btn-xs btn-primary" ng-click="editorcreateproduct()">add</a> </div> </div>
3.phone.html 页面
<div class="panel-body"> <table class="table table-striped table-hover"> <thead> <tr> <th>名称</th> <th>类别</th> <th class="text-right">价格</th> <th>{{data}}</th> </tr> </thead> <tbody> <tr ng-repeat="item in phones"> <td>{{item.name}}</td> <td>{{item.category}}</td> <td class="text-right">{{item.price | currency}}</td> <td class="text-center"> <button class="btn btn-xs btn-primary" ng-click="deleteproduct(item)">删除</button> <a href="/edit/{{item.id}}" class="btn btn-xs btn-primary" ng-click="editorcreateproduct(item)">编辑</a> <button class="btn btn-xs btn-primary" ng-click="incrementprice(item)">+</button> </td> </tr> </tbody> </table> <div> <button class="btn btn-xs btn-primary" ng-click="editorcreateproduct()">添加</button> <a href="create" class="btn btn-xs btn-primary" ng-click="editorcreateproduct()">add</a> </div> </div>
单击“首页”
单击“电脑”
单击“手机”
单击“其他”
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 详解Vuex的属性和作用