user2375298 user2375298 - 5 months ago 19
AngularJS Question

How to use local storage and delete when it is not needed

Hi i am new to angular js and i am working around anglarjs local storage technique so that data persist on refresh.
Is there any way i can delete the local storage all in one and Also assign the scope storage to a model and use the model in html than $storage.id. I m very new and please share and help me. Thank

<!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 given 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>

Answer

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

Comments