angularjs - Using angular-local-storage Inside a Service? -
i trying use angular-local-storage module (https://github.com/grevory/angular-local-storage), version 0.2.2 (according bower) in service within application. seem having issues when call within service, works fine when put inside controller.
i have configured this:
angular.module('myapp', [ 'localstoragemodule' ]) .config(function (localstorageserviceprovider) { localstorageserviceprovider .setprefix('test') .setstoragetype('localstorage'); })...
and in service, have (simplified, testing purposes):
angular.module('myapp') .service('userservice', [ 'localstorageservice', function(localstorageservice) { return { save: function() { localstorageservice.set('user', 'me'); } }; }]);
this code yields error of:
typeerror: localstorageservice.set not function
on other hand, if place exact same code controller (indeed, controller calls service), works expected. angular-local-storage not work within services?
thanks in advance!
it work me minor tweaks. see below code snippet.
angular.module('myapp', [ 'localstoragemodule' ]) .config(function (localstorageserviceprovider) { localstorageserviceprovider .setprefix('test') .setstoragetype('localstorage'); }) .service('userservice', [ 'localstorageservice', function(localstorageservice) { return { save: function() { localstorageservice.set('user', 'me'); } }; }]) .controller('mainctrl', function($scope, userservice){ $scope.test = function(){ console.debug('save called'); userservice.save(); }; });
<!doctype html> <html lang="en" ng-app="myapp"> <head> <meta charset="utf-8"> <title></title> </head> <body> <div ng-controller="mainctrl"> <button ng-click="test()">test</button> </div> <script type="text/ng-template" id="home.html"> lorem ipsum </script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> <script src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script> </body> </html>
Comments
Post a Comment