1. 程式人生 > >AngularJS路由實現單頁面跳轉

AngularJS路由實現單頁面跳轉

href vid 左邊欄 ref 按順序 -1 生活用品 func 為我

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>路由</title>
<script src="angular.min.js"></script>
<!--引入路由文件-->
<script src="https://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>

</head>
<body ng-app=‘routeDemo‘>
<!--左邊欄-->
<div id="navigator" style="width: 20%;display: inline-block;background-color: red;height: 400px;float: left">
<!--菜單-->
<ul>
<li><a href="#/home">首頁</a></li>
<li><a href="#/woman">女裝</a></li>
<li><a href="#/man">男裝</a></li>
<li><a href="#/life">生活用品</a></li>
<li><a href="#/cook">廚房用品</a></li>
</ul>
</div>
<!--右邊欄-->
<div style="width: 80%;display: inline-block;background-color: green;height: 400px;float: right">
<div ng-view=""></div>
</div>
</body>
<script type="text/ng-template" id="index.home.html">
<h1>這是首頁</h1>
</script>
<script type="text/ng-template" id="index.woman.html">
<h1>這是女裝</h1>
</script>
<script type="text/ng-template" id="index.man.html">
<h1>這是男裝</h1>
</script>
<script type="text/ng-template" id="index.life.html">
<h1>這是生活館</h1>
</script>
<script type="text/ng-template" id="index.cook.html">
<h1>這是廚房用品</h1>
</script>
<script type="text/javascript">
angular.module(‘routeDemo‘,[‘ngRoute‘])
.controller(‘HomeController‘,function ($scope,$route) {
$scope.$route = $route;
})
.controller(‘WomanController‘,function ($scope,$route) {
$scope.$route = $route;
})
.controller(‘WomanController‘,function ($scope,$route) {
$scope.$route = $route;
})
.controller(‘ManController‘,function ($scope,$route) {
$scope.$route = $route;
})
.controller(‘LifeController‘,function ($scope,$route) {
$scope.$route = $route;
})
.controller(‘CookController‘,function ($scope,$route) {
$scope.$route = $route;
})

//配置$routeProvider用來定義路由規則
//$routeProvider為我們提供了when(path,object)& other(object)函數按順序定義所有路由,函數包含兩個參數:
[email protected]
/* */:url或者url正則規則
[email protected]:路由配置對象
.config(function($routeProvider){
$routeProvider.
when(‘/home‘,{
//templateURL:插入ng-view的HTML模板文件
templateUrl:‘index.home.html‘,
controller:‘HomeController‘

}).
when(‘/woman‘,{
templateUrl:‘index.woman.html‘,
controller:‘WomanController‘
}).
when(‘/man‘,{
templateUrl:‘index.man.html‘,
controller:‘ManController‘
}).
when(‘/life‘,{
templateUrl:‘index.life.html‘,
controller:‘LifeController‘
}).
when(‘/cook‘,{
templateUrl:‘index.cook.html‘,
controller:‘CookController‘
})
})
</script>
</html>

  效果圖:

技術分享

技術分享

AngularJS路由實現單頁面跳轉