什么是SPA?看下图就是SPA:

  

  下面说正经的,个人理解SPA就是整个应用只有一个页面,所有的交互都在一个页面完成,不需要在页面之间跳转。

  单页面的好处是更快的响应速度,更流畅的用户体验,甚至和桌面应用或者原生App一样流畅。

  有很多JS框架可以用来构建SPA,Ember.js、vue.js、React、Angular等等,甚至即使你用的是jQuery开发,也有相应的框架可以用来开发SPA,比如:page.js

  本文介绍如何用Angular构建SPA,其他的依葫芦画瓢就是了,原理都差不多。

(一)Demo效果图

  

(二)代码结构

   这个Demo使用Angular和Angular Route技术实现,相应地引入了angular和angular-route两个库,先看下代码结构,有个直观的印象:

   

(三)具体实现

  1.创建不可变的布局list.html

 <!DOCTYPE html>
<html ng-app="demoApp">
<head>
<meta charset="utf-8">
<title>Angular SPA Demo</title>
<link rel="stylesheet" href="../static/css/base.css">
<link rel="stylesheet" href="../static/css/index.css">
</head>
<body>
<header class="header" ng-controller="LogoutCtrl">
<div class="left">Logo</div>
<div class="right">Hi,kagol</div>
</header>
<section class="content" ng-controller="demoListCtrl">
<aside class="wrap_nav">
<ul class="nav">
<li>
<div class="title relative"><i class="icon_fl"></i><span class="span_fl">Demo List</span></div>
<ul>
<li><a href="#/demo/list/unassign">Uncompleted</a></li>
<li><a href="#/demo/completed/processed">Completed</a></li>
<li><a href="#/demo/followup">Follow Up</a></li>
</ul>
</li>
<li>
<div class="title relative"><i class="icon_set"></i><span class="span_set">Setting</span></div>
<ul>
<li><a href="#/account">Account Info</a></li>
</ul>
</li>
</ul>
</aside>
<section class="main" ng-style="mainStyle">
<div class="inner_content" ng-view></div>
</section>
</section>
<footer class="footer">
<div class="copyright">Copyright © 1998-2016 Tencent. All Rights Reserved</div>
</footer>
<script src="../static/js/angular-1.4.8.min.js"></script>
<script src="../static/js/angular-route.min.js"></script>
<script src="../static/js/demo/demo_list/demoControllers.js"></script>
<script src="../static/js/demo/demo_list/app.js"></script>
</body>
</html>

  ng-view里面的内容是视图的内容,是可变的,其余部分是布局,不可变,这里需要注意的是一个页面只能有一个ng-view。

  2.创建可变的视图

 <!-- Uncompleted - To be Assigned -->
<script type="text/ng-template" id="list_unassign.html">
<div>To be Assigned</div>
</script> <script type="text/ng-template" id="followup.html">
<div>Follow Up</div>
</script> <!-- 收藏 --> <script type="text/ng-template" id="completed_processed.html">
<div>Completed</div>
</script> <!-- Completed demo List --> <script type="text/ng-template" id="account.html">
<div>Account</div>
</script> <!-- 帐号管理(修改密码) -->

  ng-template指令表示这是一个ng模板,id是该模板的标识,写路由规则的时候会用到。模板里可以任意发挥,编写自己需要的html内容。

  3.引入视图

  将视图代码放在包含ng-view指令的标签前面即可。

  4.创建路由规则app.js

 var demoApp = angular.module('demoApp', [
'ngRoute',
'demoControllers'
]); demoApp.config(function($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'list_unassign.html',
controller: 'demoListUnassignCtrl'
}).when('/demo/list/unassign', {
templateUrl: 'list_unassign.html',
controller: 'demoListUnassignCtrl'
}).when('/demo/completed/processed', {
templateUrl: 'completed_processed.html',
controller: 'CompletedProcessedCtrl'
}).when('/demo/followup', {
templateUrl: 'followup.html',
controller: 'FollowupCtrl'
}).when('/account', {
templateUrl: 'account.html',
controller: 'AccountCtrl'
}).otherwise({
redirectTo: '/'
});
});

  要使用Angular的路由服务,需要先引入ngRoute模块,然后使用$routeProvider服务配置路由。

  5.创建Angular控制器demoControllers.js

 var demoControllers = angular.module('demoControllers', []);

 // Logout
demoControllers.controller('LogoutCtrl', function($scope){
console.log('this is logout');
}); // Account Info
demoControllers.controller('AccountCtrl', function($scope, $routeParams){
console.log('this is account');
}); // Follow Up
demoControllers.controller('FollowupCtrl', function($scope, $http, $routeParams){
console.log('this is followup');
}); // Completed - My Processed
demoControllers.controller('CompletedProcessedCtrl', function($scope, $http, $routeParams){
console.log('this is completed');
}); // Uncompleted - To be Assigned
demoControllers.controller('demoListUnassignCtrl', function($scope, $http){
console.log('this is uncompleted');
}); demoControllers.controller('demoListCtrl', function($scope, $http){
console.log('this is demolist');
});

  控制器里可以任意发挥,编写自己需要的代码。

(四)进一步思考

  这个Demo里左侧导航点击之后并没有高亮,大家可以想想怎么实现。

使用Angular构建单页面应用(SPA)的更多相关文章

  1. PushState+Ajax实现简单的单页面应用SPA

    http://www.helloweba.com/view-blog-386.html 单页面应用(Single Page Application)简称SPA,使用SPA构建的应用优点有用户体验好.速 ...

  2. (转)前端:将网站打造成单页面应用SPA

    前端:将网站打造成单页面应用SPA(一) Coffce 680 6月19日 发布 推荐 6 推荐 收藏 85 收藏,3.1k 浏览 前言 不知你有没有发现,像Github.百度.微博等这些大站,已经不 ...

  3. 前端:将网站打造成单页面应用SPA

    前端:将网站打造成单页面应用SPA   前言 不知你有没有发现,像Github.百度.微博等这些大站,已经不再使用普通的a标签做跳转了.他们大多使用Ajax请求替代了a标签的默认跳转,然后使用HTML ...

  4. 单页面应用SPA架构

    个人认为单页面应用的优势相当明显: 前后端职责分离,架构清晰:前端进行交互逻辑,后端负责数据处理. 前后端单独开发.单独测试. 良好的交互体验,前端进行的是局部渲染.避免了不必要的跳转和重复渲染. 当 ...

  5. 借助 Vue 来构建单页面应用

    原文: https://github.com/MeCKodo/vue-tutorial 主题 Vue.js (1/2)Vue构建单页应用最佳实战 前言 我们将会选择使用一些vue周边的库 1.使用no ...

  6. MVC route 和 Angular router 单页面的一些方式

    直接看代码和注释吧 ASP.NET MVC router public class RouteConfig { public static void RegisterRoutes(RouteColle ...

  7. 单页面应用SPA和多页面应用MPA

    单页面应用(SinglePage Web Application,SPA) 只有一张Web页面的应用,是一种从Web服务器加载的富客户端,单页面跳转仅刷新局部资源 ,公共资源(js.css等)仅需加载 ...

  8. spring boot使用vue+vue-router构建单页面应用

    spring boot http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/ vue https: ...

  9. 新手vue构建单页面应用实例

    本人写的小程序,功能还在完善中,欢迎扫一扫提出宝贵意见! 步骤: 1.使用vue-cli创建项目2.使用vue-router实现单页路由3.用vuex管理我们的数据流4.使用vue-resource请 ...

随机推荐

  1. 关于C#继承运用的总结

    整体代码部分: 解决方案: 父类Person类: using System; using System.Collections.Generic; using System.Linq; using Sy ...

  2. ecshop3.0.0注入

    配个环境来演示给别人看..分析一下.flow.php文件缺陷,order_id在post请求没有单引号保护.造成注入 <?php elseif ($_REQUEST['step'] == 're ...

  3. 取汉子拼音首字母的C#和VB.Net方法

    转载http://blog.fwhyy.com/2012/03/take-the-first-initials-method-of-csharp-and-vbnet/

  4. <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies

    本题链接:http://poj.org/problem?id=2139 Description:     The cows have been making movies lately, so the ...

  5. vedio_note_1

    同步复位 always @ (posedge clk) ....... 异步复位 always @ (posedge clk or negedge rst_n) ....... 异步复位和同步复位的优 ...

  6. python3 数据类型

    Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) Number(数字) Py ...

  7. DUIlib使用Fastreport--自定义的数据

    报表根据数据源的可以分为拉模式和推模式,拉模式就是在报表中添加数据源组件从数据库中拉取数据,我们上篇报表的简单使用就是拉模式.而推模式就是在程序中构造数据托给报表显示.这篇我们这要说的是推模式. 在程 ...

  8. 【Python】考虑用生成器改写直接返回列表的函数

    使用生成器的好处是显而易见的,可以使代码更加清晰,同时减小内存的消耗,当函数需要返回列表,把函数改写为生成器是相对容易的. 下面这两个函数返回字符串中每个单词的索引: def index_words1 ...

  9. [Q]自定义纸张大小

    问:当打印机纸张列表里没有符合要求的纸张大小,例如如何打印加长图?答:当打印非标准图框时,你可能在图纸列表里找不到想要纸幅.你需要自己新建你需要的纸幅,以pdfFactory虚拟打印机为例(其它打印机 ...

  10. 如何在无法直接用VS启动代码时如何调试代码

    1. 普通情况下对进程Attach就可以调试. 2. 但是在一些情况下直接attach并无法调试,例如安装程序installer, 这样使用如下的调试方法即可调试安装程序. System.Diagno ...