Bind into property of objects.

master
Yihang Ho 10 years ago
parent 296db308bc
commit 84a9b404b6

@ -62,7 +62,7 @@ angularLocalStorage.provider('localStorageService', function() {
};
};
this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) {
this.$get = ['$rootScope', '$window', '$document', '$parse', function($rootScope, $window, $document, $parse) {
var self = this;
var prefix = self.prefix;
var cookie = self.cookie;
@ -368,7 +368,7 @@ angularLocalStorage.provider('localStorageService', function() {
value = angular.extend(def, value);
}
scope[key] = value;
$parse(key).assign(scope, value);
scope.$watchCollection(key, function(newVal) {
addToLocalStorage(key, newVal);

@ -236,6 +236,19 @@ describe('localStorageService', function() {
expect($rootScope.property).toEqual(localStorageService.get('property'));
}));
it('should be able to bind to properties of objects', inject(function($rootScope, localStorageService) {
localStorageService.set('obj.property', 'oldValue');
localStorageService.bind($rootScope, 'obj.property');
expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property'));
$rootScope.obj.property = 'newValue';
$rootScope.$digest();
expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property'));
}));
//sessionStorage
describe('SessionStorage', function() {