fix(README): getting started doc + cookies doc

master
Ariel Mashraki 10 years ago
parent 5b9cd352ec
commit ae13ec45fd

@ -6,6 +6,7 @@ An Angular module that gives you access to the browsers local storage, **v0.1.1*
[![Build Status](https://secure.travis-ci.org/grevory/angular-local-storage.png?branch=master)](https://travis-ci.org/grevory/) [![Build Status](https://secure.travis-ci.org/grevory/angular-local-storage.png?branch=master)](https://travis-ci.org/grevory/)
##Table of contents: ##Table of contents:
- [Get Started](#get-started)
- [Configuration](#configuration) - [Configuration](#configuration)
- [setPrefix](#setprefix) - [setPrefix](#setprefix)
- [setStorageType](#setstoragetype) - [setStorageType](#setstoragetype)
@ -24,7 +25,49 @@ An Angular module that gives you access to the browsers local storage, **v0.1.1*
- [bind](#bind) - [bind](#bind)
- [deriveKey](#derivekey) - [deriveKey](#derivekey)
- [length](#length) - [length](#length)
- [cookie](#cookie)
- [set](#cookie.set)
- [get](#cookie.get)
- [remove](#cookie.remove)
- [clearAll](#cookie.clearall)
##Get Started
**(1)** You can install angular-local-storage using 2 different ways:<br/>
**Git:**
clone & build [this](https://github.com/grevory/angular-local-storage.git) repository<br/>
**Bower:**
```bash
$ bower install angular-local-storage
```
**npm:**
```bash
$ npm install angular-local-storage
```
**(2)** Include `angular-local-storage.js` (or `angular-local-storage.min.js`) in your `index.html`, after including Angular itself.
**(3)** Add `'LocalStorageModule'` to your main module's list of dependencies.
When you're done, your setup should look similar to the following:
```html
<!doctype html>
<html ng-app="myApp">
<head>
</head>
<body>
...
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script src="bower_components/js/angular-local-storage.min.js"></script>
...
<script>
var myApp = angular.module('myApp', ['LocalStorageModule']);
</script>
...
</body>
</html>
```
##Configuration ##Configuration
###setPrefix ###setPrefix
You could set a prefix to avoid overwriting any local storage variables from the rest of your app<br/> You could set a prefix to avoid overwriting any local storage variables from the rest of your app<br/>
@ -132,7 +175,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) {
}); });
``` ```
###keys ###keys
Return array of keys for local storage, ignore keys that not owned. Return array of keys for local storage, ignore keys that not owned.<br/>
**Returns:** `value from local storage` **Returns:** `value from local storage`
```js ```js
myApp.controller('MainCtrl', function($scope, localStorageService) { myApp.controller('MainCtrl', function($scope, localStorageService) {
@ -207,52 +250,54 @@ myApp.controller('MainCtrl', function($scope, localStorageService) {
//... //...
}); });
``` ```
##Cookie
##Installation: Deal with browser's cookies directly.
###cookie.set
```bash Directly adds a value to cookies.<br/>
$ bower install angular-local-storage **Note:** Typically used as a fallback if local storage is not supported.<br/>
**Returns:** `Boolean`
```js
myApp.controller('MainCtrl', function($scope, localStorageService) {
//...
function submit(key, val) {
return localStorageService.cookie.set(key, value);
}
//...
});
``` ```
###cookie.get
Example use: Directly get a value from a cookie.<br/>
**Returns:** `value from local storage`
```javascript ```js
angular.module('yourModule', ['LocalStorageModule']) myApp.controller('MainCtrl', function($scope, localStorageService) {
.controller('yourCtrl', [ //...
'$scope', function getItem(key) {
'localStorageService', return localStorageService.cookie.get(key);
function($scope, localStorageService) { }
// Start fresh //...
localStorageService.clearAll(); });
// Set a key
localStorageService.set('Favorite Sport','Ultimate Frisbee');
// Delete a key
localStorageService.remove('Favorite Sport');
}]);
/*
To set the prefix of your localStorage name, you can use the setPrefix method
available on the localStorageServiceProvider
*/
angular.module('yourModule', ['LocalStorageModule'])
.config(['localStorageServiceProvider', function(localStorageServiceProvider){
localStorageServiceProvider.setPrefix('newPrefix');
}]);
``` ```
###cookie.remove
#### How to bind to a $scope variable: Remove directly value from a cookie.<br/>
Usage: localStorageService.bind(scope, scopeKey, def, lsKey); **Returns:** `Boolean`
```js
myApp.controller('MainCtrl', function($scope, localStorageService) {
//...
function removeItem(key) {
return localStorageService.cookie.remove(key);
}
//...
});
``` ```
// Example ###clearAll
$scope.anArtist = {'firstname':'Pablo', 'lastname':'Picasso'}; Remove all data for this app from cookie.<br/>
```js
// Bind to local storage service myApp.controller('MainCtrl', function($scope, localStorageService) {
localStorageService.bind($scope, 'anArtist', $scope.anArtist, 'specialArtist'); //...
function clearAll() {
// get bound data: return localStorageService.cookie.clearAll();
console.log(localStorageService.get('specialArtist')); }
});
``` ```
Check out the full demo and documentation at http://gregpike.net/demos/angular-local-storage/demo.html Check out the full demo and documentation at http://gregpike.net/demos/angular-local-storage/demo.html