diff --git a/src/angular-local-storage.js b/src/angular-local-storage.js index 47c5094..8eb430a 100644 --- a/src/angular-local-storage.js +++ b/src/angular-local-storage.js @@ -383,7 +383,7 @@ angularLocalStorage.provider('localStorageService', function() { return scope.$watch(key, function(newVal) { addToLocalStorage(lsKey, newVal); - }, isObject(scope[key])); + }, isObject(value)); }; // Return localStorageService.length diff --git a/test/spec/localStorageSpec.js b/test/spec/localStorageSpec.js index 8b134c6..04119a7 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -421,6 +421,23 @@ describe('localStorageService', function() { expect(results).toEqual(expectation); })); + it('should $watch with deep comparison for objects in nested scope properties', inject(function($rootScope, localStorageService) { + var mocks = [{}, [], 'string', 90, false]; + var expectation = [true, true, false, false, false]; + var results = []; + + spyOn($rootScope, '$watch').andCallFake(function(key, func, eq) { + results.push(eq); + }); + + mocks.forEach(function(elm, i) { + localStorageService.set('mock' + i, elm); + localStorageService.bind($rootScope, 'nested.mock' + i, null, 'mock' +i); + }); + + expect(results).toEqual(expectation); + })); + it('should be able to return it\'s owned keys amount', inject( function(localStorageService, $window) {