Skip to content

Commit 74b4248

Browse files
committed
fixed bug with setting default value to 0 or false, and added example
1 parent f2fd10c commit 74b4248

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

examples/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
5+
<script src="../components/angular/angular.min.js"></script>
6+
<script src="../components/angular-cookies/angular-cookies.min.js"></script>
7+
<script src="../src/localFactory.js"></script>
8+
<script>
9+
var eS = angular.module('exampleStore', ['localStorage']);
10+
11+
eS.controller('MainCtrl', ['$scope', '$store', function ( $scope, $store ) {
12+
$store.bind($scope, 'test', 'Some Default Text');
13+
14+
$scope.clearTest = function () {
15+
$store.remove('test');
16+
};
17+
}]);
18+
</script>
19+
</head>
20+
<body>
21+
<div ng-app="exampleStore">
22+
<div ng-controller="MainCtrl">
23+
<p>{{test}}</p>
24+
<label>Local Storage Bound:</label> <input type="text" ng-model="test">
25+
<a ng-click="clearTest()">Clear Value from LC</a>
26+
27+
<p><i>Change the value in the textbox and refresh the page!</i></p>
28+
</div>
29+
</div>
30+
</body>
31+
</html>

src/localFactory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ angular.module('localStorage', ['ngCookies']).factory('$store', function ($parse
9898
* @returns {*} - returns whatever the stored value is
9999
*/
100100
bind: function ($scope, key, def) {
101-
def = def || '';
101+
// If no defined value for def we use empty string
102+
def = (angular.isUndefined(def)) ? '' : def;
102103
if (!publicMethods.get(key)) {
103104
publicMethods.set(key, def);
104105
}

0 commit comments

Comments
 (0)