使用Yeoman搭建 AngularJS 应用 (12) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/local-storage.html
安装Bower程序包
我们使用另一个Angular模块,"angular-local-storage" 然后让我们快速的搭建一个本地存储。这次,轮到Bower来大显神通。
运行下面的命令
bower install --save angular-local-storage

添加本地存储
就像我们添加的jQuery和AngularUI Sortable那样,我们需要添加angular-local-storage.js到index.html中
我们使用Ctrl+C按键组合来退出当前的命令行应用程序,然后重新运行grunt server来自动生成index.html
在index.html底部,会添加下面的语句
<script src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
你的index.html中script段落如下所示
<!-- build:js scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-animate/angular-animate.js"></script>
<script src="bower_components/angular-touch/angular-touch.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/jquery-ui/ui/jquery-ui.js"></script>
<script src="bower_components/angular-ui-sortable/sortable.js"></script>
<script src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
<!-- endbower -->
<!-- endbuild -->
编辑mytodoApp应用程序来包含LocalStorageModule适配器 (scripts/app.js)
angular
.module('mytodoApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'ui.sortable',
'LocalStorageModule'
])
在app.js里,你也要配置ls来配置localStorageServiceProvider
.config(['localStorageServiceProvider', function(localStorageServiceProvider){
localStorageServiceProvider.setPrefix('ls');
}])
我们的应用模块现在看起来像这样
'use strict'; angular
.module('mytodoApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'ui.sortable',
'LocalStorageModule'
])
.config(['localStorageServiceProvider', function(localStorageServiceProvider){
localStorageServiceProvider.setPrefix('ls');
}])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.otherwise({
redirectTo: '/'
});
});
也需要更新控制器(main.js),添加localStorageServcice
'use strict';
angular.module('mytodoApp')
.controller('MainCtrl', function ($scope, localStorageService) {
// (code hidden here to save space)
});
现在为止,我们读取的todos是硬编码的,我们将从本地存储来获取$scope.todos来代替
我将使用angular的$watch监听来监听$scope.todos的值,如果一些人添加或者删除一个元素,它将异步的保存本地存储。
因此,我们需要删除当前的$scope.todos声明
$scope.todos = [];
替代为
var todosInStore = localStorageService.get('todos');
$scope.todos = todosInStore || [];
$scope.$watch('todos', function () {
localStorageService.set('todos', $scope.todos);
}, true);
我们的控制器现在如下所示
'use strict';
angular.module('mytodoApp')
.controller('MainCtrl', function ($scope, localStorageService) {
var todosInStore = localStorageService.get('todos');
$scope.todos = todosInStore || [];
$scope.$watch('todos', function () {
localStorageService.set('todos', $scope.todos);
}, true);
$scope.addTodo = function () {
$scope.todos.push($scope.todo);
$scope.todo = '';
};
$scope.removeTodo = function (index) {
$scope.todos.splice(index, 1);
};
});
现在打开浏览器,你将看到列表中没有值,这个app从本地存储中初始化了todos的列表,但是我们还没有存值

添加一些todo元素到todo列表中

现在,我们刷新浏览器,我们确认我们的数据是不是存在与当地存储。我们检查Chrome中的Resources选项然后选择Local Storage。

继续深造
为了拥有更强大的功能,我们可以访问下面的资源
AngularJS (angularjs.org) helped us write this Todo app quickly and with elegance. To dig deeper into the sweet spots of AngularJS, take a look at the detailed documentation.
Grunt (gruntjs.com) has tasks for almost anything you might like to do with your app, whether it’scompiling CoffeeScript or hooking up your app to custom middleware like PHP or Express. Your Yeoman app already has a Gruntfile.js already set up for you so read up on how to configure more Grunt tasks here.
Bower (bower.io) has a growing collection of easily installable packages for adding capabilities to your app. View all the packages available through Bower's registry here.
Yeoman is always evolving. Be sure to check out yeoman.io for more information and follow@yeoman and +Yeoman to stay up to date.
使用Yeoman搭建 AngularJS 应用 (12) —— 让我们搭建一个网页应用的更多相关文章
- 使用Yeoman搭建 AngularJS 应用 (5) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/scaffold-app.html 基架 (Scaffolding) 在Yeoman中的意思是为基于你特殊的配置需求,为网站程序生成文件的工 ...
- 使用Yeoman搭建 AngularJS 应用 (4) —— 让我们搭建一个网页应用
在开发一个的网页传统工作流程中,你需要大量的时间去设置引用文件,下载依赖文件,并且手动的创建网页文件结构.Yeoman生成器将会帮助你完成这些.让我们安装一个AngularJS项目的生成器. 安装An ...
- 使用Yeoman搭建 AngularJS 应用 (2) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/index.html 使用Yeoman搭建简单的应用 今天将会搭建一个简单的网页程序.你将可以添加,删除,拖拽和保存. 浏览Yeoman Y ...
- 使用Yeoman搭建 AngularJS 应用 (11) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/prepare-production.html 让我们发布这个应用 优化产品的文件 为了创建应用的产品版本,我们想做如下的事情 检查你的代码 ...
- 使用Yeoman搭建 AngularJS 应用 (10) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/write-unit-tests.html 对于不熟悉的Karma的人来说,这是JavaScript测试框架,这个Angular的生成器包含 ...
- 使用Yeoman搭建 AngularJS 应用 (9) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/install-packages.html 列出当前程序包 我们现在查看一下我们已经安装的程序包,输入下面的命令 bower list 查找 ...
- 使用Yeoman搭建 AngularJS 应用 (8) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/write-app.html 创建一个新的模板来显示一个todo的列表 打开views/main.html 为了从一个干净的模板开始,删除m ...
- 使用Yeoman搭建 AngularJS 应用 (7) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/preview-inbrowser.html 开启你的服务 运行Grunt任务,通过输入下面的命令来创建一个本地Node的http服务,地址 ...
- 使用Yeoman搭建 AngularJS 应用 (6) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/review-generated-files.html 打开mytodo文件夹,你会看到现在的基架.如下图所示 在mytodo文件夹,我们能 ...
随机推荐
- Java Concurrency - 浅析 Phaser 的用法
One of the most complex and powerful functionalities offered by the Java concurrency API is the abil ...
- 有些方法为什么会声明称static静态的
有些方法在调用的时候,没有必要都要先实例化一下,只需要:[类名. 静态方法 ]就行了. 哪些方法的调用没有必要实例化呢?网上找了个例子: 举个例子:Car类,1.静态方法Run(),Car.Run() ...
- android代码设置、打开WLAN wifi热点及热点的连接
其实创建热点很简单,先获取到wifi的服务,再配置热点名称.密码等等,然后再通过反射打开它就OK了. 下面我们看看创建热点的代码实现: 这一段是开启WLAN热点,并可以指定好它的热点名和密码 支行后, ...
- sql常识-Alias
SQL Alias 表的 SQL Alias 语法 SELECT column_name(s) FROM table_name AS alias_name 列的 SQL Alias 语法 SELECT ...
- SQL Server 2008 错误15023:当前数据库中已存在用户或角色
解决SQL Server 2008 错误15023:当前数据库中已存在用户或角色,SQLServer2008,错误15023,在使用SQL Server 2008时,我们经常会遇到一个情况:需要把一台 ...
- ASP.NET下运用Memcached
对于大型网站的高并发,在ASP.NET网站下的session性能并不高,所以造成人们一种印象,大型WEB项目使用JAVA的错觉,致使很多人吐槽微 软不给力,其实这好比拉不出怪地球引力,本文介绍Memc ...
- iOS开发——开发者官网注册新设备
1.第一步登陆苹果开发者中心官网,进入证书栏后如下图:点击All 或者如果是iPhone设备直接点击iPhone也行. 然后点击右上角的[+]号
- Entity Framework学习(一)
网上看了很多的资料,发现都不是想要的学习资料,讲的不是很明白,最后在msdn开始自己研究EF MSDN的地址 https://msdn.microsoft.com/zh-cn/library/gg69 ...
- [WCF]IIS部署到新系统
最近为以前的一个企业部署软件的时候,接触到WCF,通过博客园大佬的系列文章和一些书籍,基本了解了一些.简单说也算是SOA一种方式,提供某种服务,可以理解为一个类库,供其他项目使用,可以做到业务分离.但 ...
- java 内部类定义在局部时需要注意的情况
/*内部类定义在局部时,1,不可以被成员修饰符修饰2,可以直接访问外部类中的成员,因为还持有外部类中的引用. 但是不可以访问它所在的局部中的变量.只能访问被final修饰的局部变量.*/clas ...