单页面
SPA(Single Page Application)指的是通单一页面展示所有功能,通过Ajax动态获取数据然后进行实时渲染,结合CSS3动画模仿原生App交互,然后再进行打包(使用工具把Web应用包一个壳,这个壳本质上是浏览器)变成一个“原生”应用。
在PC端也有广泛的应用,通常情况下使用Ajax异步请求数据,然后实现内容局部刷新,局部刷新的本质是动态生成DOM,新生成的DOM元素并没有真实存在于文档中,所以当再次刷新页面时新添加的DOM元素会“丢失”,通过单页面应可以很好的解决这个问题。
路由
在后端开发中通过URL地址可以实现页面(视图)的切换,但是AngularJS是一个纯前端MVC框架,在开发单页面应用时,所有功能都在同一页面完成,所以无需切换URL地址(即不允许产生跳转),但Web应用中又经常通过链接(a标签)来更新页面(视图),当点击链接时还要阻止其向服务器发起请求,通过锚点(页内跳转)可以实现这一点。
实现单页面应用需要具备:
a、只有一页面
b、链接使用锚点
基本原理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
padding: 0;
margin: 0;
} .clearfix:after {
content: '';
display: block;
visibility: hidden;
clear: both;
} .wrap {
width: 600px;
margin: 30px auto;
} ul {
list-style: none;
border: 1px solid black;
border-bottom: none;
} li {
width: 199px;
height: 30px;
line-height: 30px;
float: left;
/*margin-left: -2px;*/
text-align: center;
position: relative;
} li a {
text-decoration: none;
color: black;
} li:after {
content: '';
display: block;
position: absolute;
width: 1px;
height: 16px;
border-right: 1px solid black;
top: 7px;
right: 0px;
} li:last-child:after {
border: none;
} .wrap .main {
height: 400px;
border: 1px solid #000;
text-align: center;
line-height: 400px;
}
</style>
</head>
<body>
<div class="wrap">
<ul class="clearfix">
<li><a href="#index1">index1</a></li>
<li><a href="#index2">index2</a></li>
<li><a href="#index3">index3</a></li>
</ul>
<div class="main">
</div>
</div>
<script>
//js有一个监听锚点变化的事件 hashchange
window.addEventListener('hashchange', function (ev) {
var hash = location.hash;
hash = hash.slice(1);
console.log(hash);
var xhr = new XMLHttpRequest();
xhr.open('get', '01.php?hash=' + hash);
xhr.send(null);
xhr.onreadystatechange = function (ev2) {
if (xhr.readyState == 4 && xhr.status == 200) {
document.querySelector('.main').innerHTML = xhr.responseText;
}
}
})
</script>
</body>
</html>
通过上面的例子发现在单一页面中可以能过hashchange事件监听到锚点的变化,进而可以实现为不同的锚点准不同的视图,单页面应用就是基于这一原理实现的。AngularJS对这一实现原理进行了封装,将锚点的变化封装成路由(Route),这是与后端路由的根本区别。
具体使用
在angular的路由模块中,也实现了此功能,其原理如上。route模块之前是包括在angular核心代码中,后来分离出来,需要单独去引用才能使用
1、引入angular-route.js
2、实例化模块(App)时,当成依赖传进去(模块名称叫ngRoute)
3、配置路由模块
4、布局模板,通过ng-view指令布局模板,路由匹配的视图会被加载渲染到些区域。
路由参数:
when只需要两个参数,otherwise需要一个参数
1、when的第1个参数是一个字符串,代表当前URL中的hash值。
2、when的第2个参数是一个对象,配置当前路由的参数,如视图、控制器等。
a、template 字符串形式的视图模板
b、templateUrl 引入外部视图模板
c、controller 视图模板所属的控制器
d、redirectTo跳转到其它路由
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
padding: 0;
margin: 0;
} .clearfix:after {
content: '';
display: block;
visibility: hidden;
clear: both;
} .wrap {
width: 600px;
margin: 30px auto;
} ul {
list-style: none;
border: 1px solid black;
border-bottom: none;
} li {
width: 199px;
height: 30px;
line-height: 30px;
float: left;
/*margin-left: -2px;*/
text-align: center;
position: relative;
} li a {
text-decoration: none;
color: black;
} li:after {
content: '';
display: block;
position: absolute;
width: 1px;
height: 16px;
border-right: 1px solid black;
top: 7px;
right: 0px;
} li:last-child:after {
border: none;
} .wrap .main {
height: 400px;
border: 1px solid #000;
text-align: center;
line-height: 400px;
}
</style>
</head>
<body>
<div class="wrap">
<ul class="clearfix">
<li><a href="#/index1">index1</a></li>
<li><a href="#/index2">index2</a></li>
<li><a href="#/index3">index3</a></li>
</ul>
<div class="main">
<div ng-view=""></div>
</div>
</div>
<script src="../libs/angular.min.js"></script>
<script src="../libs/angular-route.js"></script>
<script>
//之前路由模块也是处于核心模块之中,后来独立出去了
//对路由模块的使用主要是进行config配置,配置完成之后即可使用
var App = angular.module('App', ['ngRoute']);
App.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/index1', {
template: '<h1>index1</h1>'
}).when('/index2', {
template: '<h1>index2</h1>'
}).when('/index3', {
template: '<h1>index3</h1>'
}).otherwise({
redirectTo: '/index1'
})
}]);
</script>
</body>
</html>