什么是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#-实验3

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. C# 利用ajax实现局部刷新

    C#所有runat="server"的控件都会造成整个界面的刷新,如果想实现局部刷新,可以利用ajax. 需要加入的控件有ScriptManager和UpdatePanel,可以实 ...

  3. python新手 实践操作 作业

    #有如下值集合 [11,22,33,44,55,66,77,88,99],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: {'k1': 大于66的所 ...

  4. 面试题-Java Web-网络通信

    1.HTTP响应的结构是怎么样的? HTTP响应由三个部分组成:状态码(Status Code):描述了响应的状态.可以用来检查是否成功的完成了请求.请求失败的情况下,状态码可用来找出失败的原因.如果 ...

  5. ORACLE AWR性能报告和ASH性能报告的解读

    数据库的性能分析可分为会话级和系统级:如果确定某个会话存在性能问题,最常见的分析方式是对这个会话做一个SQL_TRACE或者10046事件,通过分析trace文件来定位问题所在.如果无法确定哪个会话性 ...

  6. PHP学习过程_Symfony_(4)_命令创建实体_以及实体关系

    //项目运行php app/console server:run//创建实体php app/console doctrine:generate:entitybundle名称:实体名称例如:Symfon ...

  7. 【IIS】windows2008 ii7 设置访问网站提示帐号密码登录

    3个步骤: 1.添加windows身份验证: windows2008默认是不启用的,需要我们自己去启动,在管理工具 - 服务器管理- 角色 ,拉下去,下面有个[添加角色服务],安全性- Windows ...

  8. Centos 下安装Zabbix Linux 客户端

    今天在linux上安装了客户端,过程如下: (1)下载zabbix客户端软件 wget www.zabbix.com/downloads/2.0.3/zabbix_agents_2.0.3.linux ...

  9. Wise Registry Cleaner Pro(智能注册表清理) V9.31 绿色版

    软件名称: Wise Registry Cleaner Pro(智能注册表清理)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大 ...

  10. shell的入门

    shell :弱类型. 解释型语言 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 ...