I want to register actions in my angular application.
If I cannot send the message, I want to add the item to another local storage log so that the next time I send the message, I can try and add these items.
Once this is done, I want to try to post the current items. I also want to have a (sync) button that goes through the process without going through the action log process. so that the user can try to publish all the elements that they could not publish before.
Iām thinking of approaching him like that. on submit
-add to local storage, then try sending a message. (so that it first places the previous items)
of success
-change item from local storage
on error
-support item in local storage
I am a glorious way down the road to getting this job, as shown below, however I'm not sure if this is the best way to get close to this.
At the moment, I can publish success data, but not delete a single item from local storage.
I added the removeName name to userService, which I commented out in my lower code, as it currently removes _nameLog, and not a link to the local repository file.
But when you run the code in the codepen demo , does it publish every element every time, since I do not delete them on successful completion?
How can I delete the local repository item on successful execution without deleting _namelog (since this is necessary to continue in ng-repeat), or is there another way I should get close to this?
<body ng-app="myApp"> <div ng-controller="MyCtrl"> <input type="text" ng-model="updatedname"> <input type="button" value="Change name" ng-click="changeName(updatedname)"/> <br/> Hello, {{name}}! <ul> <li ng-repeat="name in nameLog">{{name.value}} - {{name.time}}</li> </ul> <input type="button" value="sync" ng-click="syncPosts()"/> </div> </body> <script> var myApp = angular.module('myApp',[]); myApp.factory('UserService', ['$window','$http', function($window,$http) { var _nameLog = []; var userService = {}; userService.name = "John"; userService.ChangeName = function (value) { userService.name = value; }; userService.logName = function (value) { _nameLog.push ({ "value":value, "time" :Date.now() }); }; userService.removeName = function (value) { return delete _nameLog[0]; }; userService.getNameLog = function(){ return _nameLog; }; userService.setLS = function(key, value) { $window.localStorage[key] = value; }, userService.getLS = function(key, defaultValue) { return $window.localStorage[key] || defaultValue; }; userService.setObject = function(key, value) { $window.localStorage[key] = JSON.stringify(value); }; userService.getObject = function(key) { return JSON.parse($window.localStorage[key] || '{}'); }; userService.testPost = function(myVal,myTime) { return $http.post('http://jsonplaceholder.typicode.com/posts', {title:myVal,body:myTime,userId: 1}); }; return userService; }]); function MyCtrl($scope, UserService) { $scope.name = UserService.name; $scope.updatedname=""; $scope.changeName=function(data){ $scope.updateServiceName(data); } $scope.updateServiceName = function(name){ UserService.ChangeName(name); UserService.logName(name); $scope.name = UserService.name; $scope.nameLog = UserService.getNameLog(); UserService.setLS('name', JSON.stringify($scope.nameLog)); getPosts(); } $scope.syncPosts = function(){ getPosts(); } function testPost(myVal,myTime) { UserService.testPost(myVal,myTime) .success(function(data, status, headers, config) { console.log('success'); console.log(data); </script>