How to use local storage and delete when not needed

Hi, I am new to angular js and I am working on a local anglarjs storage technique so that the data is saved when updating. Is there a way to remove local storage in one, as well as assign area storage for the model and use the model in html than $ storage.id. I am very new, and please share and help me. thanks

<!DOCTYPE html> <html ng-app="myApp"> <head> <title> </title> <script type="text/javascript" src="angular.min.js"></script> <script type="text/javascript" src="angular-local-storage.js"></script> <script type="text/javascript" src="angular-route.js"> </script> <script> var myApp=angular.module("myApp",['ngStorage','ngRoute']); myApp.controller('myAppCtrl', ['$scope','$localStorage','$location', function($scope,$localStorage,$location){ $scope.$storage=$localStorage.$default({ myname:"", myid:"", mynumber:"", }); $scope.add=function() { alert("asdsd") delete $scope.$storage.myid; delete $scope.$storage.myname; delete $scope.$storage.mynumber; $location.path("/add"); } }]); myApp.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider) { $routeProvider. when('/', { templateUrl: 'search.html', controller:'myAppCtrl' }). when('/add',{ templateUrl:'add.html', controller:'myAppCtrl' }). otherwise({ redirectTo: '/' }); }]); </script> </head> <body > <div ng-view> 

and my template is listed below.

 <script type="text/ng-template" id="search.html"> <button ng-click="add();"> add </button> </script> <script type="text/ng-template" id="add.html"> <div> <input type="text" ng-model="$storage.myname"/></br> <input type="text" ng-model="$storage.myid"/></br> <input type="number" ng-model="$storage.mynumber"/></br> <button ng-click="submit();"> submit </button> <button ng-click="edit();"> view button </button> </div> </script> </body> </html> 
+6
source share
2 answers

you can use $localStorage.$reset(); , it will delete all your data from localStorage

+22
source

To remove local storage in one, use $ localStorage.clearAll ().

To manually add a key / value pair to local storage, use $ localStorage.add ('id', $ scope.id). Or, to bind directly to a scope variable, use $ localStorage.bind ($ scope, 'id', id).

To get the value from local storage, use $ localStorage.get ('id').

The GitHub ReadMe documentation explains this: https://github.com/grevory/angular-local-storage

+1
source

Source: https://habr.com/ru/post/974261/


All Articles