agrublev/angularLocalStorage

语言: JavaScript

git: https://github.com/agrublev/angularLocalStorage

您将使用的最简单的角度localStorage实现。
The simplest angular localStorage implementation you will ever use.
README.md (中文)

angularLocalStorage

您将使用的最简单的本地存储模块。允许您设置,获取和绑定变量。

特征:

  • 双向绑定$ scope变量值到localStorage键/对,每当模型更新时,它都会更新。
  • 您可以直接存储对象,数组,浮点数,布尔值和字符串。无需从字符串转换您的javascript值。
  • 如果不支持localStorage,则回退到Angular $ cookieStore(记住将angular-cookies.min.js脚本添加到项目中或从依赖项中删除'ngCookies');

如何使用

  1. 只需将此模块作为依赖项添加到您的应用程序 var yourApp = angular.module('yourApp',[...,'angularLocalStorage']
  2. 现在你的控制器内部就像这样传递存储工厂 yourApp.controller('yourController',function($ scope,storage){
  3. 使用存储工厂   ```JAVASCRIPT   //注意:如果范围变量已存在,则它优先于默认值和存储值   //将它绑定到$ scope.variable(最小)   storage.bind($范围, '的varName');   //绑定完整   storage.bind($ scope,'varName',{defaultValue:'randomValue123',storeName:'customStoreKey'});   //参数是($ scope,varName,opts(可选))   // $ scope - 将引用传递给变量所在的任何范围   // varName - 变量名称,因此对于$ scope.firstName输入'firstName'   // opts - 自定义选项,如默认值或唯一商店名称   //以下是您可以设置的可用选项:   // * defaultValue:默认值   // * storeName:添加自定义商店键值,而不是使用范围变量名称

//将不断更新$ scope.viewType   //在控制器本地更改变量,在localStorage中更改变量   $ scope.viewType ='ANYTHING';   //就是这样,它将在localStorage中更新

//只需在localStorage中存储一些内容,并为不支持的浏览器添加cookie备份   storage.set( '键', '值');   //得到那个价值   storage.get( '钥匙');

//获取未设置的密钥将返回null   console.log(storage.get('keyThatIsUndefined'))// null

//检查是否正在使用cookie回退,因此您不要尝试在cookie中存储相当大的数据   if(!storage.isCookieFallbackActive()){     ...   }

//清除所有localStorage值   storage.clearAll();

//获取作为数组返回的存储系统中的所有密钥   storage.getKeys(); // ['key','key2']   ```

鲍尔

该模块作为bower包提供,使用以下命令安装:

bower install angularLocalStorage

要么

bower install git://github.com/agrublev/angularLocalStorage.git

角度要求

0.3.0需要AngularJS 1.3.0+。

0.2.0需要AngularJS 1.2.x.

对于实例,请结帐 - http://plnkr.co/edit/PNLjDEaRKtpLgGZMJypk?p=preview

建议?

请添加想法,改进或错误的问题!谢谢!


(c)2015年麻省理工学院执照

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

angularLocalStorage Build Status

The simpliest localStorage module you will ever use. Allowing you to set, get, and bind variables.

Features:

  • Two way bind your $scope variable value to a localStorage key/pair which will be updated whenever the model is updated.
  • You can directly store Objects, Arrays, Floats, Booleans, and Strings. No need to convert your javascript values from strings.
  • Fallback to Angular $cookieStore if localStorage is not supported (REMEMBER to add angular-cookies.min.js script to your project or remove 'ngCookies' from a dependency);

How to use

  1. Just add this module to your app as a dependency
    var yourApp = angular.module('yourApp', [..., 'angularLocalStorage']
  2. Now inside your controllers simply pass the storage factory like this
    yourApp.controller('yourController', function( $scope, storage){
  3. Using the storage factory
    ```JAVASCRIPT
    // Note: if the scope variable already exists, it takes precedence over the default and stored values
    // binding it to a $scope.variable (minimal)
    storage.bind($scope,'varName');
    // binding full
    storage.bind($scope,'varName',{defaultValue: 'randomValue123' ,storeName: 'customStoreKey'});
    // the params are ($scope, varName, opts(optional))
    // $scope - pass a reference to whatever scope the variable resides in
    // varName - the variable name so for $scope.firstName enter 'firstName'
    // opts - custom options like default value or unique store name
    // Here are the available options you can set:
    // * defaultValue: the default value
    // * storeName: add a custom store key value instead of using the scope variable name

// will constantly be updating $scope.viewType
// to change the variable both locally in your controller and in localStorage just do
$scope.viewType = 'ANYTHING';
// that's it, it will be updated in localStorage

// just storing something in localStorage with cookie backup for unsupported browsers
storage.set('key','value');
// getting that value
storage.get('key');

// Getting an unset key will return null
console.log(storage.get('keyThatIsUndefined')) // null

// checking if the cookie fallback is being used right now, so you don't try to store fairly big data in cookies
if(!storage.isCookieFallbackActive()) {
...
}

// clear all localStorage values
storage.clearAll();

// gets all the keys in the storage system returned as an array
storage.getKeys(); // ['key','key2']
```

Bower

This module is available as bower package, install it with this command:

bower install angularLocalStorage

or

bower install git://github.com/agrublev/angularLocalStorage.git

Angular Requirements

0.3.0 requires AngularJS 1.3.0+.

0.2.0 requires AngularJS 1.2.x.

Example

For live example please checkout - http://plnkr.co/edit/PNLjDEaRKtpLgGZMJypk?p=preview

Suggestions?

Please add an issue with ideas, improvements, or bugs! Thanks!


(c) 2015 MIT License